Check sensors every hour?

Hi everyone! I have 2 Zigbee temperature sensors (Sonoff), 4 BLE sensors (Xiaomi, flashed with ATC fw) and one Z-Wave (Samsung).

Some of them are going offline randomly and I’d like to troubleshoot this. For this, I’m asking for help on the best way to create an automation or a Node-Red flow that notifies me if any of these sensors haven’t updated their states in over an hour.
Most of them don’t become “unavailable” when they stop working, so I need this notification to react on devices not reporting values over a certain amount of time.

I can think of an Inject node that activates hourly and stores the temperature of each device in a flow variable, and then the next hour it checks the device’s temperature vs the previously stored variable. Does this make any sense? Can anyone think of something better?

Thanks,
Rodrigo

trigger:
  - platform: state
    entity_id: <entity_id_here_>
    to:
    for:
      hours: 1
1 Like

Potential node red solution

1 Like

Thanks @tom_l for the reply. Unfortunately it didn’t work. I tried this adding an automation from the Automations page, I first added the notification using the UI, then went to the YAML editor to paste your code and edit the entity_id (I changed the hours to minutes just to test it faster):

alias: Notify Unchanged Living Temp X
description: ""
trigger:
  - platform: state
    entity_id: sensor.ble_temperature_living_temp_x
    to:
    for:
      minutes: 1
condition: []
action:
  - service: notify.mobile_app_pixel_6a
    data:
      message: Living Temp X Unchanged
mode: single

After saving it, if I enter again to the automation editor it says “to: null”. And even though the sensor updates its temperature every 5 minutes, the automation was never triggered.

In the meantime, I explored more and ended up finding something similar to what @MaxK said.
It worked fine but I was worried about copying and pasting many times.
When using WebCoRE, I would create automations in ways that were easily “scalable” and editable. So I created a Node-RED flow (with the help of ChatGPT for the Function node) that has all the device IDs listed in the inject node (which executes every 1 hour), a Function node that sends these deviceIDs in separate messages, and a Current State node that checks the timeSinceChanged value.

[{"id":"9c7389cccd519965","type":"function","z":"a3a03ad94d09ee01","name":"function 1","func":"var entityIds = msg.payload.split(\",\");\nvar i = 0;\n\nfunction sendEntity() {\n    var entityId = entityIds[i];\n    var newMsg = { payload: entityId };\n    node.send(newMsg);\n    i++;\n    if (i < entityIds.length) {\n        setTimeout(sendEntity, 2000);\n    }\n}\nsendEntity();","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":920,"wires":[["4af6293b4d8ac84a"]]},{"id":"3883cbe19fe60980","type":"inject","z":"a3a03ad94d09ee01","name":"","props":[{"p":"payload"}],"repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"sensor.ble_temperature_bedroom_temp_x,sensor.ble_temperature_living_temp_x,sensor.ble_temperature_kitchen_temp_x,sensor.ble_temperature_study_temp_x,sensor.bathroom_temp_s_temperature,sensor.living_temp_s_temperature,sensor.water_leak_temperature","payloadType":"str","x":150,"y":920,"wires":[["9c7389cccd519965"]]},{"id":"866ee38723b25939","type":"link out","z":"a3a03ad94d09ee01","name":"link out 11","mode":"link","links":["3357c1ef43290083"],"x":705,"y":920,"wires":[]},{"id":"4af6293b4d8ac84a","type":"api-current-state","z":"a3a03ad94d09ee01","name":"","server":"8a86b0cc.b2bac","version":3,"outputs":2,"halt_if":"$entity().timeSinceChangedMs > 3600000","halt_if_type":"jsonata","halt_if_compare":"jsonata","entity_id":"{{payload}}","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":530,"y":920,"wires":[["866ee38723b25939"],[]]},{"id":"8a86b0cc.b2bac","type":"server","name":"Home Assistant R","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}]

So this starts another question:
Is it problematic in any way to have these automations running inside Node-RED? (Instead of HA automations). I don’t plan to do things that are much more complex. Do I need to worry about performance or any other issues? I understand that Node-RED is a third party and then this could mean trouble at some point, but apart from that, is there any reason to worry?

Thanks,
Rodrigo

If you are using the UI automation editor you need to supply hours, minutes and seconds, even if they are 0.

There is very little I cant do natively in Home Assistant so I find Node Red to be needless overhead. But if you prefer it then there are no real performance issues to worry about.

1 Like

If you are trying to troubleshoot, I assumed you were looking for a short term solution to resolve your hardware issues.

If you are looking for a “scalable” solution, I would recommend getting better sensors with integrations that have an Integration Quality Scale of Silver or better. Then you can build on something like this for a long-term solution:

{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | map(attribute='entity_id') | list | join(', ') }}

Concerning which is better: native Home Assistant or Node-Red? I prefer chocolate :slightly_smiling_face:

Take a look at this repo. I use this to monitor zwave devices, could easily be ported to other transports

Ok, I tried it and it still says “last triggered: never” :frowning:

I’m still interested on understanding how to do these by code! But it’s not working…

alias: Notify Unchanged Living Temp X 2
description: ""
trigger:
  - platform: state
    entity_id: sensor.ble_temperature_living_temp_x
    to: null
    for:
      hours: 1
action:
  - service: notify.mobile_app_pixel_6a
    data:
      message: Living Temp X Unchanged

I removed the battery of the device last night, the node-red automation worked perfectly but I cannot get this one to work yet (I am keeping trying with this one just to learn a bit if possible).

Thank you,
R.

This doesn’t need to be a rock-solid network, if it works it helps me do some things in more comfortable ways, but there’s no risk here.
I plan to keep this automation working so it tells me if any device is malfunctioning. For example, I noticed that if I put a fully charged LIR2025 battery (around 4v) on my Sonoff motion sensors, they will malfunction, and they will only work fine if the battery is below 3.5v.
My BLE devices are disconnecting but most of the times I believe it is because of a signal/positioning thing. I’ll get an ESP32 and also I hope I’ll get my Home Assistant Yellow some day, maybe these hardware changes improve reliability for these devices, but in the meantime I’m tracking their status and deciding what to do with them. When a device fail I note its position, its battery voltage and also if I had done something recently. It’s not a big issue for me right now, the idea is to maybe have them working better.

I don’t plan on spending more money on temperature sensors, since they’re a thing I like but I don’t need at all. I only have two temperature-sensor-related automations, and if I’m having trouble with most devices, I can get rid of them and only keep the ones that function better.

Yea, of courde. It’s just that I started using SmartThings Hub and a water leak sensor a long time ago, when I had a water leaking issue and needed this device to notify me in case of emergencies. This hardware is what pushed me to start learning about automations (with WebCoRE later). And in a couple of years, I added lights, switches and blinds controllers. I was using WebCoRE and the users in the forums started saying that it would be deprecated later at some point. And years later this is finally happening and I’m enjoying the switch to Node-RED.
I just want to make sure there isn’t a red flag running about Node-RED and that I’m not going to be forced to migrate all my automations again :laughing:

Cheers!
Rodrigo