ottopaulsen / node-red-contrib-power-saver

A Node-RED node to saver money by turning off when the power is most expensive
Other
71 stars 17 forks source link

Lowest price always sends on/true until first period #184

Open malgoe opened 10 months ago

malgoe commented 10 months ago

Lowest Price node The schedule generated after a reboot sets output to on/true even though I have set "Outside Period, Send: Off". After the first period has started everything works as expected until i restart Node Red. I don't think this is because of missing data right after restart since schedule is updated on the hourly price changes and it still looks the same until first period started.

Adding output below. Note this is generated at 16.08PM. The first schedule row I would expect to have value:false. The schedule for tomorrow looks as expected (off between 05.00 - 20.00).

{"schedule":[{"time":"2023-11-09T00:00:00+01:00","value":true,"countHours":20},{"time":"2023-11-09T20:00:00+01:00","value":false,"countHours":5},{"time":"2023-11-10T01:00:00+01:00","value":true,"countHours":4},{"time":"2023-11-10T05:00:00+01:00","value":false,"countHours":15},{"time":"2023-11-10T20:00:00+01:00","value":true,"countHours":4}],"hours":[{"start":"2023-11-09T00:00:00+01:00","price":0.485,"onOff":true,"saving":null},{"start":"2023-11-09T01:00:00+01:00","price":0.429,"onOff":true,"saving":null},{"start":"2023-11-09T02:00:00+01:00","price":0.397,"onOff":true,"saving":null},{"start":"2023-11-09T03:00:00+01:00","price":0.373,"onOff":true,"saving":null},{"start":"2023-11-09T04:00:00+01:00","price":0.374,"onOff":true,"saving":null},{"start":"2023-11-09T05:00:00+01:00","price":0.389,"onOff":true,"saving":null},{"start":"2023-11-09T06:00:00+01:00","price":0.461,"onOff":true,"saving":null},{"start":"2023-11-09T07:00:00+01:00","price":0.528,"onOff":true,"saving":null},{"start":"2023-11-09T08:00:00+01:00","price":0.538,"onOff":true,"saving":null},{"start":"2023-11-09T09:00:00+01:00","price":0.558,"onOff":true,"saving":null},{"start":"2023-11-09T10:00:00+01:00","price":0.558,"onOff":true,"saving":null},{"start":"2023-11-09T11:00:00+01:00","price":0.598,"onOff":true,"saving":null},{"start":"2023-11-09T12:00:00+01:00","price":0.61,"onOff":true,"saving":null},{"start":"2023-11-09T13:00:00+01:00","price":0.688,"onOff":true,"saving":null},{"start":"2023-11-09T14:00:00+01:00","price":0.743,"onOff":true,"saving":null},{"start":"2023-11-09T15:00:00+01:00","price":0.763,"onOff":true,"saving":null},{"start":"2023-11-09T16:00:00+01:00","price":1.342,"onOff":true,"saving":null},{"start":"2023-11-09T17:00:00+01:00","price":1.377,"onOff":true,"saving":null},{"start":"2023-11-09T18:00:00+01:00","price":1.363,"onOff":true,"saving":null},{"start":"2023-11-09T19:00:00+01:00","price":1.237,"onOff":true,"saving":null},{"start":"2023-11-09T20:00:00+01:00","price":0.568,"onOff":false,"saving":null},{"start":"2023-11-09T21:00:00+01:00","price":0.542,"onOff":false,"saving":null},{"start":"2023-11-09T22:00:00+01:00","price":0.484,"onOff":false,"saving":null},{"start":"2023-11-09T23:00:00+01:00","price":0.415,"onOff":false,"saving":null},{"start":"2023-11-10T00:00:00+01:00","price":0.404,"onOff":false,"saving":null},{"start":"2023-11-10T01:00:00+01:00","price":0.363,"onOff":true,"saving":null},{"start":"2023-11-10T02:00:00+01:00","price":0.344,"onOff":true,"saving":null},{"start":"2023-11-10T03:00:00+01:00","price":0.331,"onOff":true,"saving":null},{"start":"2023-11-10T04:00:00+01:00","price":0.327,"onOff":true,"saving":null},{"start":"2023-11-10T05:00:00+01:00","price":0.363,"onOff":false,"saving":null},{"start":"2023-11-10T06:00:00+01:00","price":0.436,"onOff":false,"saving":null},{"start":"2023-11-10T07:00:00+01:00","price":0.912,"onOff":false,"saving":null},{"start":"2023-11-10T08:00:00+01:00","price":1.186,"onOff":false,"saving":null},{"start":"2023-11-10T09:00:00+01:00","price":1.165,"onOff":false,"saving":null},{"start":"2023-11-10T10:00:00+01:00","price":1.149,"onOff":false,"saving":null},{"start":"2023-11-10T11:00:00+01:00","price":1.133,"onOff":false,"saving":null},{"start":"2023-11-10T12:00:00+01:00","price":1.017,"onOff":false,"saving":null},{"start":"2023-11-10T13:00:00+01:00","price":0.757,"onOff":false,"saving":null},{"start":"2023-11-10T14:00:00+01:00","price":0.654,"onOff":false,"saving":null},{"start":"2023-11-10T15:00:00+01:00","price":1.279,"onOff":false,"saving":null},{"start":"2023-11-10T16:00:00+01:00","price":0.688,"onOff":false,"saving":null},{"start":"2023-11-10T17:00:00+01:00","price":1.597,"onOff":false,"saving":null},{"start":"2023-11-10T18:00:00+01:00","price":0.487,"onOff":false,"saving":null},{"start":"2023-11-10T19:00:00+01:00","price":0.405,"onOff":false,"saving":null},{"start":"2023-11-10T20:00:00+01:00","price":0.36,"onOff":true,"saving":null},{"start":"2023-11-10T21:00:00+01:00","price":0.33,"onOff":true,"saving":null},{"start":"2023-11-10T22:00:00+01:00","price":0.32,"onOff":true,"saving":null},{"start":"2023-11-10T23:00:00+01:00","price":0.283,"onOff":true,"saving":null}],"source":"Nordpool","config":{"fromTime":"20","toTime":"05","hoursOn":4,"maxPrice":null,"doNotSplit":false,"sendCurrentValueWhenRescheduling":true,"outputIfNoSchedule":true,"outputOutsidePeriod":false,"outputValueForOn":true,"outputValueForOff":false,"outputValueForOntype":"bool","outputValueForOfftype":"bool","override":"auto","contextStorage":"memory","hasChanged":false},"time":"2023-11-09T16:08:07.158+01:00","version":"4.1.4","strategyNodeId":"0e6b0d58dab3f3a2","current":true}

malgoe commented 10 months ago

Exported the nodes in question, just in case:

[{"id":"49a6573d3b5c33c8","type":"api-current-state","z":"16e241f.434c0be","name":"Hämta elpris","server":"b19aacc5.be737","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.nordpool_kwh_se4_sek_3_10_025","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":350,"y":880,"wires":[["64647373c3801d80"]]},{"id":"64647373c3801d80","type":"ps-receive-price","z":"16e241f.434c0be","name":"Price Receiver","x":540,"y":880,"wires":[["0e6b0d58dab3f3a2"]]},{"id":"0e6b0d58dab3f3a2","type":"ps-strategy-lowest-price","z":"16e241f.434c0be","name":"Lowest Price","fromTime":"20","toTime":"05","hoursOn":"4","maxPrice":"","doNotSplit":false,"sendCurrentValueWhenRescheduling":true,"outputValueForOn":"true","outputValueForOff":"false","outputValueForOntype":"bool","outputValueForOfftype":"bool","outputIfNoSchedule":"true","outputOutsidePeriod":"false","contextStorage":"memory","x":730,"y":880,"wires":[["3bd694b85a4b9830"],["4fbb113b2cedd735"],["d5eb369949efcc8d"]]},{"id":"fec14b5046aba64e","type":"server-state-changed","z":"16e241f.434c0be","name":"Nordpool price change","server":"b19aacc5.be737","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"sensor.nordpool_kwh_se4_sek_3_10_025","entityIdType":"exact","outputInitially":true,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":880,"wires":[["49a6573d3b5c33c8"]]},{"id":"b19aacc5.be737","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

malgoe commented 10 months ago

I noticed when using "Deploy" i often get errors from the Lowest price-node: "No price data" and "TypeError: Cannot destructure property 'plan' of 'handleStrategyInput(...)' as it is undefined." after these the schedule is generated as in my previous post.

ottopaulsen commented 10 months ago

I notice this in your config: "outputValueForOn":true. I believe that is the reason you get the output on after reboot. That is the "If no schedule, send" setting.

If you have it configured, you could try to set contextStorage to file. Then it is supposed to reload the previous schedule and continue with whatever value that was planned. I am not sure how well that works though.

About theNo price data that is of course correct, but the TypeError could have been avoided with better code.


Edit: I wrote "outputValueForOn":true above, but I ment "outputIfNoSchedule":true

malgoe commented 10 months ago

But this whole thing is triggered by the event state node for my nordpool sensor. Why would it not have price data?

ottopaulsen commented 10 months ago

The errors come when the node starts up. That is before any input. Initial output is also set then.

malgoe commented 10 months ago

(Thank you for spending time on helping me out here, I really appreciate it!)

Then why is the schedule still wrong when the next hour and price change comes?

ottopaulsen commented 10 months ago

Now I see your point. I think that is a bug. Thanks to your input I have been able to create a test, currently failing. I will try to find time to fix it soon. Thank you for reporting.

ottopaulsen commented 10 months ago

I have found a bug and I will publish a fix soon (in v 4.1.5). However, it may not give exactly the result you expect. It will still output true for the first hour. Actually the first 5 hours. That is because when the data starts, at midnight, and your planning period is from 20 to 05, then at midnight you are inside your planning period, and you are missing data from 20 to 00. When data is missing, the node cannot plan that period, so for the period from 00 to 05, there will be no schedule. Then the outputIfNoSchedule value will be used, and you have set this to true.

(I have edited my comment above about this value. I wrote outputValueForOn where I ment outputIfNoSchedule).

The bug was that the schedule continued to be on after 05, but since that is outside the planning period, your outputOutsidePeriod value should be used. The bug fixes that.

malgoe commented 10 months ago

Updated to 4.1.5 and outputIfNoSchedulestill seems to take precendence over outputOutsidePeriodoutside the period. I restarted Node Red after updating.

Also: If i try to update the schedule after 13 (that's when nordpool releases prices for the next day) I do have price data for my whole period (20-05), shouldn't that work?

ottopaulsen commented 10 months ago

I must see the output to be able to evaluate the case.

If you have data for today and tomorrow, you can see the schedule for tonight, but not for tomorrow night.