Trigger from Node-Red if a sensor doesn't change for X hours

Doing some more searching I found this:

1 Like

Maybe you could put a delay node on the start of your flow.
The delay node can be set to hold back the message output until the timer in its setting runs out, but with a reset each time a new message arrive.
This way the messages should be blocked in the delay node until no updates is received and the delay node then release its message.

How do you mean? Start with a delay node, without anything going into it?
Use “Override delay with msg.delay” in some way?
What after the delay node? A current state node? But how to feed anything in to the delay node then?
I think something in your description sounds reasonable…but I don’t understand how to do…

A lot of code…a little bit too much for me…not really know where to start to understand. Want a lot, but feels like a long way to go to learn, but I must start somewhere…but from what I understand most of this is regarding binary sensors. Can it be applied to a thermometer as well?
Late here now…trying to get some time to read it again and try again tomorrow

I agree with WallyR. all presented solutions so far are over-engineered while what you want can be achieved without anything special but built-in delay node which will measure the time for you and publish message when time exceeds

use output of Events:state node to feed a Delay node. in between of them you have to set the message properly in order to reset the delay node.(use change node to add ‘reset’ property to the message)

This way every incoming message will reset the Delay node starting measuring time from the beginning ). If message will not come within specified time, delay node will publish the last stored message. Use it to trigger notification (the message contains all you need, the last value, name of entity etc)

you may set state node to not publish message if previews and new states are equal. But tbh I think getting the same values proves your device is working. So I wouldn’t include that condition

I think the second link I provided is a simple solution that requires no code (just a polling node). I tested it with one of my temperature sensors and it works as expected.

Entities have a timeSinceChangedMs attribute measured in milliseconds. If that value is greater than 86400000 (24 hours) the state has not changed in 24 hours.

So, you can poll timeSinceChangedMs at some interval (60 seconds or whatever) and check to see if that value is greater than the time you want (not changed in 24 hours = true?).

A working example. Modify the Entity Id, Update Interval, and milliseconds (If State) for your environment. You can replace the debug 1 node with your notification.

image

[{"id":"bddbd75a.af7608","type":"poll-state","z":"8b43de8505f17be7","name":"","server":"f3f422ce.9f2a6","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"10","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.basement_flood_temperature","state_type":"str","halt_if":"$entity().timeSinceChangedMs > 86400000","halt_if_type":"jsonata","halt_if_compare":"jsonata","outputs":2,"x":700,"y":300,"wires":[["1df3de1bd2af0c7b"],["69fcee022f51c44b"]]},{"id":"1df3de1bd2af0c7b","type":"debug","z":"8b43de8505f17be7","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":280,"wires":[]},{"id":"69fcee022f51c44b","type":"debug","z":"8b43de8505f17be7","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":340,"wires":[]},{"id":"f3f422ce.9f2a6","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}]

I tried this and swapped entity id for one of my sensors:
image

But it looks like it’s not sending the timeSinceChangedMs message

image

Those sensors are pretty simple and stupid. It looks like poll state only reads last value from the database.
Maybe this only works for smarter sensors? What kind of sensor did you try on?

Big thanks for all effort trying to help!

maybe OT, but I saw now that after every deply I get a warning I have unconfigured nodes and it looks to be Home Assistant it warns about…

image

Something to bother about or can I do something about it

Edit:
Some googling and I think it was another Home Assistant Server node was inserted with the import of the poll state code…deleted now and back to normal

Hmmm…tried a bit
It works to have the Event:state node to send out every message that comes from the detector.

Then I configured the output it like this:


Added reset as output in the event:state node (isn’t that what a change node would do?)

But on every message where the delay node is reset, the message is discarded, so there is no message left to send if time runs out…?

You have probably imported some nodes at a time and a server config node was included.
It does not mean much here, because the number is 0, so no other nodes need it and you can then just doubleclick on it and choose delete.

Yes - I realized that and deleted. Probelem solved

I have tested a bit…and could not get it working that way as if I included reset into the Event:State message it just deleted it.
But when I did like this it looks to work (with a test sensor I can control):


The first Event:State node sends the event state (and some other info) into the delay node
The second Event:State node sends msg.reset (set to “reset”, but could be anyting)
Then reset node holds the message with the entity state and on every consecutive state that comes it also comes a reset deleting the former one…(?)
Need to test a bit more, but looks promising

Edit/Add:
It works!
Now I have also managed to pass working information to Notification and Email nodes!
Next step is to figure out how much of the code I need to duplicate to supervise several sensors!
So fun to learn.
Thanks for helping!!!

1 Like

That’s correct - timeSinceChangedMs is the last time the database recorded data from the sensor. So, if the database has not recorded data from the sensor for x milliseconds… we have not heard from that sensor for x milliseconds

It’s not suppose to send that message. node: debug 2 is showing the output for when the If State condition is FALSE (we have data from the sensor that is less than x milliseconds). It’s working as intended.

Aha! I think I understand! I have to try to setup this again and play around a bit more.
Trying the other way now with two Event:State, the first sending the value, the other sending reset and that looks to work. Playing around with notifications and email now and I think I start to get hold of how to format with change nod to put together strings from multiple inputs and free text :slight_smile:

Now I’m thinking about what will happen if I try to monitor multiple sensors (I want to check at least for (fridges and freezers) but maybe also my light sensor (rebuilt thermomerter/humidity sendor where humidity port is used for light) and maybe also an outdoor temp/hum and a few indor temp/hum…

Edit: With Event:State strategy I might need to duplicate everything (2xEvent:State+Delay) except the notificaiton part (change+notify+change+email) for every sensor…

This is definitely beyond my previously knowledge - but it’s great fun to learn! And what would
I have done without fourm…googling/reading helps a bit, but when not understanding this forum is brilliant :slight_smile:

It works!!! And I think I understand why!
Now I have to try to build with this strategy as well!
Have to figure out where to input sensor ID and last read value and then pass it to notificaiton and email.
Will never learn without struggling!

Thanks!

Edit/Add: If I can figure out how to add my needed outputs (used msg.sensor_id and msg.sensor_value as self defined outputs to avoid mixing them with defaults and be sure to understand where debug info comes from) somewhare/in any way - then I might only need one PollState block for each sensor I want to check and I can poll inly few times a day, like every 12h and the process will very low intense (might not matter…to read a sensor is probably not that intense for a computer…but still… :slight_smile: )

Add2: msg.payload and msg.topic is in the PollState output! Then I just have to format it and send to my Notifier and Email nodes!

Thanks again!

A better long-term solution is to get better sensors that work with Home Assistant integrations that have an Integration Quality Scale of Silver or better so that you can detect a state change when the entity goes “unavailable.” For example (I am not necessarily recommending Shelly though I do use it):

image

Ummm…I have looked at for example SHELLY-HT-WH that is supposed to live up to 18 mounth on a CR 123 battery and costs (only) about half of the 433MHz temp/hum sensors I (already) have.
The ones I have lives >4-5 year on 2xAA batteries, so they are very cheap in operation and have worked very well apart from that I have had to check now and then to see they still reports and that if two of them start to send simultaneously it can take a few hours before I get a reading again, but I can live with that.
I have 9 of them in operation…so I will prioritize to change 433MHz switches first. When budget allows, I might start swapping sensors as well.

But with the help I have got from you and WallyR in this forum it looks like I will be able to make the situation much more secure with my old sensors. Maybe not as good as with more modern ones, but probably well good enough!

Maybe this question starts to come OT, but it’s somewhat related…
I have thought about PollState node for another purpose.
I can see my phones battery percent as a sensor, but it is only updated when screen is unlocked.

I started trying to build a flow that switch of a plug for my charger when battery reach a set procentage (an input number helper)
As battery percent sensor is only updated when screen is unlocked, I tried the PollState node and that could fetch the value even if screen was locked.
But I don’t want node to poll that sensor 24/7, only while charging, but there is no input to the node.

You used the “If State” with the elegant expression:
entity().timeSinceChangedMs > 28800000
If I understand correct, it reads an entity in the database and if that is > X ms it returns true.

I tried to swap that entity for a toggle switch (just grabbed one for a lamp)
image
(Title should be Phone Charing, Entity ID should be battery percentage sensor and the entity of the plug feedign the charger in If State, ofcause)

image

and set = on…connected debug nodes to both outlets, but message payload comes from true independant of state of that lamp switch…
Is it possible to use PollState for what I want?

I did not test this but I think you want something like this:

image

[{"id":"8a8dc7dcc5eab749","type":"trigger-state","z":"8b43de8505f17be7","name":"Check Phone Battery > 85% & Switch","server":"","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.marks_pixel_battery_power","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"85"},{"targetType":"entity_id","targetValue":"switch.shelly_plug_1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"ON"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":250,"y":700,"wires":[["5434357890c29262"],[]]},{"id":"5434357890c29262","type":"api-call-service","z":"8b43de8505f17be7","name":"","server":"","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.shelly_plug_1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":580,"y":700,"wires":[[]]}]

There is also some good information in this guide and a cookbook for different things you might like to try:

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/

Are you sure about that?
The Poll State node just pulls the state from HA.
If HA has no new information, then you just get the old one.