Timed inject node fires on HA restart

I have a timed inject node that fires once each day and creates sensor data.

Unfortunately, it also fires everytime I restart home assistant. It then creates a database entry with a NULL value.

How can I get rid of this? I need my sensor to have exactly one database entry per day, no matter what.

Few things, you should always include your flow when posting

This sensor is created how? mqtt, entity node?

Nodered does not restart with ha. You would need to either restart the supervisor or the whole system from the supervisor page. Is that how you are restarting or just from the server page?

This seems like HA is not storing the state, when you restart ha the node used to create the sensor, is there a message with the value under the node?

Thank you for the advice. So, here is the flow:

[{"id":"21dfaf285434adc7","type":"tab","label":"Create Average Sensor Data","disabled":false,"info":"","env":[]},{"id":"43d4a83ff8a15b53","type":"inject","z":"21dfaf285434adc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"59 23 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":320,"wires":[["e8dc2b52101ab6a0"]]},{"id":"e8dc2b52101ab6a0","type":"api-get-history","z":"21dfaf285434adc7","name":"","server":"c6196f00.64307","version":0,"startdate":"","enddate":"","entityid":"sensor.shellys_1_on_time","entityidtype":"is","useRelativeTime":true,"relativeTime":"23h 59m","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":440,"y":380,"wires":[["2f488e0d9cb42ed7"]]},{"id":"2f488e0d9cb42ed7","type":"function","z":"21dfaf285434adc7","name":"","func":"msg.payload = msg.payload[msg.payload.length-1].state;\nnode.warn(msg.payload);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":360,"wires":[["238e0ead9b52c6f9"]]},{"id":"238e0ead9b52c6f9","type":"ha-entity","z":"21dfaf285434adc7","name":"shellys_1_historic","server":"c6196f00.64307","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"shellys_1_historic"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"h"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":880,"y":360,"wires":[[]]},{"id":"c6196f00.64307","type":"server","name":"Home Assistant","version":2,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

the function:

msg.payload = msg.payload[msg.payload.length-1].state;
node.warn(msg.payload);
return msg;

Entity node.

Just from the server controls:
Screenshot 2022-01-26 214200

Nope, just some information when it registered (see screenshot above).

If I can give any more information, just fire away!

My server is down right now, so I can’t look. You probably looked, but just in case, does that node have an ‘output on connect’ option that you can disable?

Just rechecked the DB, seems like I made a little mistake: the additional database entries aren’t NULL, they always have the value that was set in the last “regular” run. Here’s a little screenshot to clarify:


Only the marked entries come from regular runs, as you can see from the timestamp, which correlates to the inject node. All other entries come from restarts (I’m restarting quite often, because I’m installing a lot of addons and changing some settings). Nevertheless, the question remains the same.

Well, not so sure about that, but that does look a little bit suspicious.

Screenshot 2022-01-26 221157

“Output on connect” isn’t available as an option when you select “inject at specific time”. Enable show debug on the entity node. If the state was changed from nodered you’ll see the state null under the node.

image

Otherwise it will read registered at the time HA restarts. Restart ha and see what it says and if there is an entry in the db. If there is an entry,



Edit. Before disconnecting the node you’ll need to set a value besides null to the sensor.



disconnect the wire to the entity but leave the entity floating, deploy and restart. I think this is still happening in HA and not nodered.

Btw when you deploy in nodered, there is a drop down menu on the deploy button. Choose deploy modified nodes only.

Thanks for all the replies. I played around a bit with various settings and here is what I figured out so far.

Since i disabled “Resend State And Attributes”, I don’t have any duplicate database entries anymore.

BUT I also discovered that when I restart just the core using the command line (ha core stop / ha core start), it creates a database entry with sensor value “unknown”.

I tried implementing a switch node that checks if the sensor value is a number, but that didn’t work (the unknown entries were still created).

However, since those values aren’t displayed in the history graph, I can live with that for now. It would just be perfect if I could keep the database clean, but then again, I almost never restart the core using the command line.

It’s kinda round about but you could use a write file node to create a text file, then import that to the database. This is caused by the way HA is handling the sensor. I can tell you that if it’s kept in nodered it will only fire once a day regardless if NR/HA is restarted.

The other thing is I use influx, there are dedicated nodes that I can write directly to the db without using HA. I’m sure there are nodes for sql etc.

https://flows.nodered.org/search?type=node