From ff490af167b61136d3f8f94e181c3d07a1ee377d Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Sat, 17 Oct 2020 10:46:43 +0100 Subject: [PATCH 1/9] Fix schedule calculation --- demandshaper-module/demandshaper_model.php | 5 +++++ forecasts/nordpool.php | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/demandshaper-module/demandshaper_model.php b/demandshaper-module/demandshaper_model.php index f07a291..4751be0 100644 --- a/demandshaper-module/demandshaper_model.php +++ b/demandshaper-module/demandshaper_model.php @@ -299,7 +299,9 @@ public function get_combined_forecast($config,$timezone) { $params->start = floor($now/$params->interval)*$params->interval; $params->end = $params->start + (3600*24); + // initial assumed profile length $profile_length = ($params->end-$params->start)/$params->interval; + $combined = false; foreach ($config as $config_item) { $name = $config_item->name; @@ -318,6 +320,9 @@ public function get_combined_forecast($config,$timezone) { // Fetch forecast $fn = "get_forecast_$name"; if ($forecast = $fn($this->redis,$params)) { + // calculate actual profile length + $profile_length = count($forecast->profile); + // Clone first, combine 2nd, 3rd etc if ($combined==false) { $combined = clone $forecast; diff --git a/forecasts/nordpool.php b/forecasts/nordpool.php index b84ca9f..b47eb0b 100644 --- a/forecasts/nordpool.php +++ b/forecasts/nordpool.php @@ -93,7 +93,7 @@ function get_forecast_nordpool($redis,$params) $timevalues[$timestamp] = number_format($row->value*$vat*0.1,3,'.',''); } } - + // 3. Map forecast to request start, end and interval $profile = array(); for ($time=$params->start; $time<$params->end; $time+=$params->interval) { @@ -101,15 +101,20 @@ function get_forecast_nordpool($redis,$params) if (isset($timevalues[$forecast_time])) { $value = $timevalues[$forecast_time]; - } else if (isset($timevalues[$forecast_time-(24*3600)])) { // if not available try to use value 24h in past - $value = $timevalues[$forecast_time-(24*3600)]; - } else if (isset($timevalues[$forecast_time+(24*3600)])) { // if not available try to use value 24h in future - $value = $timevalues[$forecast_time+(24*3600)]; + $last_time_value = $forecast_time; + } else if (isset($timevalues[$forecast_time+(3600)])) { // if not available, this may be feed that only shows forecasts starting from next hour + $value = $timevalues[$forecast_time+(3600)]; + } else { + $value = null; } - $profile[] = 1*$value; + $profile[] = $value == null ? null : 1*$value; } + // remove empty array items + $profile = array_filter($profile); + // adjust end value accordingly + $params->end = $last_time_value; $result = new stdClass(); $result->start = $params->start; From 4f2e866245128515db624a79880d113e14d6e67e Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 21 Oct 2020 12:40:23 +0100 Subject: [PATCH 2/9] Revert "Fix schedule calculation" This reverts commit ff490af167b61136d3f8f94e181c3d07a1ee377d. --- demandshaper-module/demandshaper_model.php | 5 ----- forecasts/nordpool.php | 17 ++++++----------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/demandshaper-module/demandshaper_model.php b/demandshaper-module/demandshaper_model.php index 4751be0..f07a291 100644 --- a/demandshaper-module/demandshaper_model.php +++ b/demandshaper-module/demandshaper_model.php @@ -299,9 +299,7 @@ public function get_combined_forecast($config,$timezone) { $params->start = floor($now/$params->interval)*$params->interval; $params->end = $params->start + (3600*24); - // initial assumed profile length $profile_length = ($params->end-$params->start)/$params->interval; - $combined = false; foreach ($config as $config_item) { $name = $config_item->name; @@ -320,9 +318,6 @@ public function get_combined_forecast($config,$timezone) { // Fetch forecast $fn = "get_forecast_$name"; if ($forecast = $fn($this->redis,$params)) { - // calculate actual profile length - $profile_length = count($forecast->profile); - // Clone first, combine 2nd, 3rd etc if ($combined==false) { $combined = clone $forecast; diff --git a/forecasts/nordpool.php b/forecasts/nordpool.php index b47eb0b..b84ca9f 100644 --- a/forecasts/nordpool.php +++ b/forecasts/nordpool.php @@ -93,7 +93,7 @@ function get_forecast_nordpool($redis,$params) $timevalues[$timestamp] = number_format($row->value*$vat*0.1,3,'.',''); } } - + // 3. Map forecast to request start, end and interval $profile = array(); for ($time=$params->start; $time<$params->end; $time+=$params->interval) { @@ -101,20 +101,15 @@ function get_forecast_nordpool($redis,$params) if (isset($timevalues[$forecast_time])) { $value = $timevalues[$forecast_time]; - $last_time_value = $forecast_time; - } else if (isset($timevalues[$forecast_time+(3600)])) { // if not available, this may be feed that only shows forecasts starting from next hour - $value = $timevalues[$forecast_time+(3600)]; - } else { - $value = null; + } else if (isset($timevalues[$forecast_time-(24*3600)])) { // if not available try to use value 24h in past + $value = $timevalues[$forecast_time-(24*3600)]; + } else if (isset($timevalues[$forecast_time+(24*3600)])) { // if not available try to use value 24h in future + $value = $timevalues[$forecast_time+(24*3600)]; } - $profile[] = $value == null ? null : 1*$value; + $profile[] = 1*$value; } - // remove empty array items - $profile = array_filter($profile); - // adjust end value accordingly - $params->end = $last_time_value; $result = new stdClass(); $result->start = $params->start; From 91f1189842d9bb8f45ef7ed54289738aea3a40be Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 13 Oct 2021 18:15:30 +0100 Subject: [PATCH 3/9] Fix nordpool scheduling --- .vscode/launch.json | 49 ++++++++++++++++++++++++++++++++++++++++++ demandshaper_run.php | 2 +- forecasts/nordpool.php | 20 +++++++++-------- 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0e70ba5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,49 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "name": "Listen for Xdebug", + "type": "php", + "request": "launch", + "port": 9000 + }, + { + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 9000, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, + { + "name": "Launch Built-in web server", + "type": "php", + "request": "launch", + "runtimeArgs": [ + "-dxdebug.mode=debug", + "-dxdebug.start_with_request=yes", + "-S", + "localhost:0" + ], + "program": "", + "cwd": "${workspaceRoot}", + "port": 9000, + "serverReadyAction": { + "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", + "uriFormat": "http://localhost:%s", + "action": "openExternally" + } + } + ] +} \ No newline at end of file diff --git a/demandshaper_run.php b/demandshaper_run.php index aea662d..e81638f 100644 --- a/demandshaper_run.php +++ b/demandshaper_run.php @@ -237,7 +237,7 @@ if ($schedule->settings->ctrlmode=="smart") { // If the schedule has not yet started it is ok to recalculate the schedule periods to find a more optimum time - if (!isset($schedule->runtime->started) || $schedule->settings->interruptible) { + if (!isset($schedule->runtime->started) || !$schedule->runtime->started || $schedule->settings->interruptible) { // Automatic update of time left for schedule e.g take into account updated battery SOC of electric car, home battery, device $schedule = $device_class[$device_type]->auto_update_timeleft($schedule); diff --git a/forecasts/nordpool.php b/forecasts/nordpool.php index b84ca9f..db1a2fc 100644 --- a/forecasts/nordpool.php +++ b/forecasts/nordpool.php @@ -75,8 +75,12 @@ function get_forecast_nordpool($redis,$params) "t"=>time() ); if ($result = http_request("GET","http://datafeed.expektra.se/datafeed.svc/spotprice",$req_params)) { - $redis->set($key,$result); - $redis->expire($key,3600); + $ob = json_decode($result); + // only store valid JSON + if($ob != null) { + $redis->set($key,$result); + $redis->expire($key,3600); + } } } $result = json_decode($result); @@ -100,14 +104,12 @@ function get_forecast_nordpool($redis,$params) $forecast_time = floor($time / $forecast_interval) * $forecast_interval; if (isset($timevalues[$forecast_time])) { - $value = $timevalues[$forecast_time]; - } else if (isset($timevalues[$forecast_time-(24*3600)])) { // if not available try to use value 24h in past - $value = $timevalues[$forecast_time-(24*3600)]; - } else if (isset($timevalues[$forecast_time+(24*3600)])) { // if not available try to use value 24h in future - $value = $timevalues[$forecast_time+(24*3600)]; + $value = $timevalues[$forecast_time]; + } else { + $value = null; } - - $profile[] = 1*$value; + + $profile[] = $value == null ? null : 1*$value; } From 313baaf9a4e62ed283f3882e2cc4ff1b65a18b45 Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 10 Nov 2021 07:26:02 +0000 Subject: [PATCH 4/9] Fix NordPool Spot value scheduling --- .vscode/launch.json | 5 +++++ demandshaper-module/demandshaper_model.php | 14 ++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0e70ba5..eb73a64 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -24,6 +24,11 @@ "env": { "XDEBUG_MODE": "debug,develop", "XDEBUG_CONFIG": "client_port=${port}" + }, + "xdebugSettings": { + "max_children": 128, + "max_data": 512, + "max_depth": 3 } }, { diff --git a/demandshaper-module/demandshaper_model.php b/demandshaper-module/demandshaper_model.php index f07a291..a7c7899 100644 --- a/demandshaper-module/demandshaper_model.php +++ b/demandshaper-module/demandshaper_model.php @@ -321,13 +321,19 @@ public function get_combined_forecast($config,$timezone) { // Clone first, combine 2nd, 3rd etc if ($combined==false) { $combined = clone $forecast; - for ($td=0; $td<$profile_length; $td++) $combined->profile[$td] = 0; + + // do not set NordPool nulls to 0 as the values don't really exist + if($name!="nordpool") { + for ($td=0; $td<$profile_length; $td++) $combined->profile[$td] = 0; + } } - // Combine + // Combine, but ignore null values for ($td=0; $td<$profile_length; $td++) { - $combined->profile[$td] += ($forecast->profile[$td]*$config_item->weight); - } + if(isset($forecast->profile[$td])) { + $combined->profile[$td] += ($forecast->profile[$td]*$config_item->weight); + } + } } } } From 3658d6d9a6ad746a4a631c3904614cbdc98fca77 Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 10 Nov 2021 07:36:52 +0000 Subject: [PATCH 5/9] Update gitignore for VS Code --- .gitignore | 1 + .vscode/launch.json | 54 --------------------------------------------- 2 files changed, 1 insertion(+), 54 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 1bb3020..1072663 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ cli/settings.php +!.vscode/launch.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index eb73a64..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - - { - "name": "Listen for Xdebug", - "type": "php", - "request": "launch", - "port": 9000 - }, - { - "name": "Launch currently open script", - "type": "php", - "request": "launch", - "program": "${file}", - "cwd": "${fileDirname}", - "port": 9000, - "runtimeArgs": [ - "-dxdebug.start_with_request=yes" - ], - "env": { - "XDEBUG_MODE": "debug,develop", - "XDEBUG_CONFIG": "client_port=${port}" - }, - "xdebugSettings": { - "max_children": 128, - "max_data": 512, - "max_depth": 3 - } - }, - { - "name": "Launch Built-in web server", - "type": "php", - "request": "launch", - "runtimeArgs": [ - "-dxdebug.mode=debug", - "-dxdebug.start_with_request=yes", - "-S", - "localhost:0" - ], - "program": "", - "cwd": "${workspaceRoot}", - "port": 9000, - "serverReadyAction": { - "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", - "uriFormat": "http://localhost:%s", - "action": "openExternally" - } - } - ] -} \ No newline at end of file From 24a1f38493db4c33371a2876bf8703a84dd0ec1d Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 10 Nov 2021 07:37:42 +0000 Subject: [PATCH 6/9] Ignore launch.json --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1072663..faaf3fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ cli/settings.php -!.vscode/launch.json \ No newline at end of file +.vscode/launch.json \ No newline at end of file From 22612ccbdd4ed63d6b78438c39c760b3b809f769 Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 29 Dec 2021 13:25:07 +0000 Subject: [PATCH 7/9] Fix combined forecast duplication Add support for Nordpool tax and seller margin --- demandshaper-module/demandshaper_model.php | 18 +++++++++++------- forecasts/nordpool.php | 6 ++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/demandshaper-module/demandshaper_model.php b/demandshaper-module/demandshaper_model.php index a7c7899..9fed049 100644 --- a/demandshaper-module/demandshaper_model.php +++ b/demandshaper-module/demandshaper_model.php @@ -326,14 +326,18 @@ public function get_combined_forecast($config,$timezone) { if($name!="nordpool") { for ($td=0; $td<$profile_length; $td++) $combined->profile[$td] = 0; } - } - - // Combine, but ignore null values - for ($td=0; $td<$profile_length; $td++) { - if(isset($forecast->profile[$td])) { - $combined->profile[$td] += ($forecast->profile[$td]*$config_item->weight); + } + else { + for ($td=0; $td<$profile_length; $td++) { + if(isset($forecast->profile[$td])) { + if($combined->profile[$td] == null) { + $combined->profile[$td] = 0; + } + + $combined->profile[$td] += ($forecast->profile[$td]*$config_item->weight); + } } - } + } } } } diff --git a/forecasts/nordpool.php b/forecasts/nordpool.php index db1a2fc..9c6e5af 100644 --- a/forecasts/nordpool.php +++ b/forecasts/nordpool.php @@ -47,7 +47,7 @@ function get_forecast_nordpool($redis,$params) "DK1"=>array("currency"=>"DKK","vat"=>"25"), "DK2"=>array("currency"=>"DKK","vat"=>"25"), "EE"=>array("currency"=>"EUR","vat"=>"20"), - "FI"=>array("currency"=>"EUR","vat"=>"24"), + "FI"=>array("currency"=>"EUR","vat"=>0,"tax"=>2.79372,"sellermargin"=>0.17), "LT"=>array("currency"=>"EUR","vat"=>"21"), "NO1"=>array("currency"=>"NOK","vat"=>"25"), "NO2"=>array("currency"=>"NOK","vat"=>"25"), @@ -90,11 +90,13 @@ function get_forecast_nordpool($redis,$params) $timevalues = array(); if ($result!=null && isset($result->data)) { $vat = (100.0+$nordpool[$params->area]["vat"])/100.0; + $tax = isset($nordpool[$params->area]["tax"]) ? $nordpool[$params->area]["tax"] : 0; + $sellermargin = isset($nordpool[$params->area]["sellermargin"]) ? $nordpool[$params->area]["sellermargin"] : 0; foreach ($result->data as $row) { $date = new DateTime($row->utc); $date->setTimezone($timezone); $timestamp = $date->getTimestamp(); - $timevalues[$timestamp] = number_format($row->value*$vat*0.1,3,'.',''); + $timevalues[$timestamp] = number_format($row->value*$vat*0.1+$tax+$sellermargin,3,'.',''); } } From d822e9a48400c47dc79be96b17e791040c4af11d Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Wed, 23 Feb 2022 18:57:45 +0000 Subject: [PATCH 8/9] Fix timer not updating --- devices/openevse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devices/openevse.php b/devices/openevse.php index 88f8414..09d587d 100644 --- a/devices/openevse.php +++ b/devices/openevse.php @@ -74,11 +74,11 @@ public function timer($device,$s1,$e1,$s2,$e2) { $timer_str = time_conv_dec_str($s1," ")." ".time_conv_dec_str($e1," "); if (!isset($this->last_timer[$device])) $this->last_timer[$device] = ""; - if ($timer_str!=$this->last_timer[$device]) { + //if ($timer_str!=$this->last_timer[$device]) { $this->last_timer[$device] = $timer_str; $this->mqtt_client->publish("$device/rapi/in/\$ST",$timer_str,0); schedule_log("$device set timer $timer_str"); - } + //} } public function set_divert_mode($device,$mode) { From c05da2bd8993ff79f0ada975644330bf3371b8d2 Mon Sep 17 00:00:00 2001 From: Jussi Palo Date: Thu, 19 May 2022 06:33:47 +0100 Subject: [PATCH 9/9] Undo timer set --- devices/openevse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devices/openevse.php b/devices/openevse.php index 09d587d..88f8414 100644 --- a/devices/openevse.php +++ b/devices/openevse.php @@ -74,11 +74,11 @@ public function timer($device,$s1,$e1,$s2,$e2) { $timer_str = time_conv_dec_str($s1," ")." ".time_conv_dec_str($e1," "); if (!isset($this->last_timer[$device])) $this->last_timer[$device] = ""; - //if ($timer_str!=$this->last_timer[$device]) { + if ($timer_str!=$this->last_timer[$device]) { $this->last_timer[$device] = $timer_str; $this->mqtt_client->publish("$device/rapi/in/\$ST",$timer_str,0); schedule_log("$device set timer $timer_str"); - //} + } } public function set_divert_mode($device,$mode) {