Get sensor value from specific date in past

Hello,
I need to read sensor for total photovoltaic export to grid from certain datetime from past. This value I need to subtract from current export.
Problem is, that I am invoiced from february and not able to set custom dates in energy page.

So is it possible to read sensor value b ase on specific datetime?
current value I am able to read whithin function:

var todayExport = Number(global.get(“homeassistant”).homeAssistant.states[“sensor.tpj5clg01x_export_to_grid_energy_total”].state);

but I need to read value at specific date/time

thank you.

The get history node.

Sample flow:

As Mikefila says, you can use the Get history node.

This node can be a little tricky to set up and to process the return (usually an array) so I have put an example flow together that may help you. If you pass an identical startDate and endDate as the boundary time period, the Get History node should return an array of just one item with the state value as it was at that exact timestamp.

The following flow will allow you to set the timestamp in the inject node (this must be ISO format) and it will read both the history and the current state value, and do the calculation for you.

Note that Home Assistant stores history for a default 10 days only, so if you try to read earlier than this the result will be ‘undefined’ as there is no history to return.

[{"id":"17a1219784662b39","type":"api-get-history","z":"776c027950fc8c3f","name":"Read history","server":"","version":1,"startDate":"","endDate":"","entityId":"","entityIdType":"equals","useRelativeTime":false,"relativeTime":"","flatten":true,"outputType":"array","outputLocationType":"msg","outputLocation":"payload","x":570,"y":6260,"wires":[["a8a6ad3739951f80"]]},{"id":"11b6d6430440b56f","type":"inject","z":"776c027950fc8c3f","name":"Set datetime","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"(\t    $time:= \"2024-02-10T18:00\";\t    {\"startDate\": $time,\t     \"endDate\":   $time }\t)","payloadType":"jsonata","x":190,"y":6260,"wires":[["0ae6472768e514d8"]]},{"id":"8214b83dbbb1ca36","type":"debug","z":"776c027950fc8c3f","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":6260,"wires":[]},{"id":"0ae6472768e514d8","type":"api-current-state","z":"776c027950fc8c3f","name":"Get current state","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.help_ri_grid_energy_export","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"payload ~> |$|{\"entityId\": $entity().entity_id}|","valueType":"jsonata"},{"property":"state","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":370,"y":6260,"wires":[["17a1219784662b39"]]},{"id":"a8a6ad3739951f80","type":"change","z":"776c027950fc8c3f","name":"Calculate difference","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(state - $number(payload[0].state),3)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":6260,"wires":[["8214b83dbbb1ca36"]]}]

The flow has been scrubbed to remove the HA server, so you will need to edit the node configuration to put this back again, and of course set you own sensor ID.

I have made no allowance for time zone differences, and HA will store history using UTC, so you may have to experiment a little with the timestamp to get this right.

Yes, you can read the current state from the Global context homeassistant variable, but I have included both a current state node and a get history node together here. The timestamp is set in the inject node (you can therefore set this from a variable if you like). The entity id is set in the current state node, then captured and passed to the get history node (so you only have to set it in one place). The calculation is done in a change node using JSONata.

I hope this helps!

1 Like

@Mikefila @Biscuit
thank you.
But get history node returns only historic data limited by “purge_keep_days” parameter = 10 days default.
I need to get sensor value older, so get history node is useless for reading older data than 10 days.

I need to get “total production” of my photovoltaic in past and subtract it from current “total production” so I get exact production during certain (year) billing period.

I am thinking about some scheduling task, which store each day value into MYSQL and then it is simple to read that value with some node-red flow, which set some “sensor” value.

thank you

That is because you are not looking for history, you are looking for long term statistics. It’s 2 different things in HA. Unfortunately there is no easy way to get this data.

It requires specific sql queries and a data generator.