Advertisement
seston

Nordpooli boiler v2. Hinnapiiriga

Jul 9th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. let CONFIG = {
  2. api_endpoint: "https://dashboard.elering.ee/api/nps/price/ee/current",
  3. switchId: 0, // ID of the switch to control
  4. price_limit: 70, // EUR/MWh. Vat not included
  5. update_time: 60000, // 1 minute. Price update interval in milliseconds
  6. reverse_switching: false // If true, switch will be turned on when price is over the limit
  7. };
  8.  
  9. let current_price = null;
  10. let last_hour = null;
  11. let last_price = null;
  12. let state = null;
  13.  
  14. function getCurrentPrice() {
  15. Shelly.call(
  16. "http.get",
  17. {
  18. url: CONFIG.api_endpoint,
  19. },
  20. function (response, error_code, error_message) {
  21. if (error_code !== 0) {
  22. print(error_message);
  23. return;
  24. }
  25. current_price = JSON.parse(response.body).data[0]["price"];
  26. print("Updated current price!");
  27. }
  28. );
  29. }
  30.  
  31. function changeSwitchState(state) {
  32. let state = state;
  33.  
  34. if(state === false) {
  35. print("Switching off!");
  36. } else if(state === true) {
  37. print("Switching on!");
  38. } else {
  39. print("Unknown state");
  40. }
  41.  
  42. Shelly.call(
  43. "Switch.Set",
  44. {
  45. id: CONFIG.switchId,
  46. on: state,
  47. },
  48. function (response, error_code, error_message) {
  49. if (error_code !== 0) {
  50. print(error_message);
  51. return;
  52. }
  53. }
  54. );
  55. }
  56.  
  57. Timer.set(CONFIG.update_time, true, function (userdata) {
  58. Shelly.call("Sys.GetStatus", {}, function (resp, error_code, error_message) {
  59. if (error_code !== 0) {
  60. print(error_message);
  61. return;
  62. } else {
  63. let hour = resp.time[0] + resp.time[1];
  64. //update prices
  65. if (last_hour !== hour) {
  66. print("update hour");
  67. last_hour = hour;
  68. getCurrentPrice();
  69. }
  70.  
  71. //check if current price is set
  72. if (current_price !== null) {
  73.  
  74. //Normal switching. Turn relay off if price is over the limit
  75. if(CONFIG.reverse_switching === false) {
  76. if (current_price >= CONFIG.price_limit) {
  77. //swith relay off if price is higher than limit
  78. changeSwitchState(false);
  79. } else {
  80. //swith relay on if price is lower than limit
  81. changeSwitchState(true);
  82. }
  83. }
  84.  
  85. //Reverse switching. Turn relay on if price is over the limit
  86. if(CONFIG.reverse_switching === true) {
  87. if (current_price >= CONFIG.price_limit) {
  88. //swith relay on if price is higher than limit
  89. changeSwitchState(true);
  90. } else {
  91. //swith relay off if price is lower than limit
  92. changeSwitchState(false);
  93. }
  94. }
  95.  
  96. } else {
  97. print("Current price is null. Waiting for price update!");
  98. }
  99.  
  100.  
  101. print(current_price);
  102. }
  103. });
  104. });
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement