Can I script this with some sort of for loop over an array of states?

Hi,

Seasoned programmer, but n00b with HA here:

I have this simple automation for spotting if a battery sensor has not been seen for a prescribed period.
Is there a way to put the condition and action into an iterator loop over a set of sensors?

Basically I want to iterate the whole condition/action unit over a set of 2 dozen devices, replacing the string: sensor.genius_valve_21 from values from a list.

Ignore the testing values of /10 timing and > 280 seconds - these are so I can see it working. In reality, I’d test perhaps once every hour and look for stuff that had not responded in that time.

Thank you kindly for any pointers where I should start…

Here’s the code:

id: '1623586635906'
alias: Check Batteries
description: ''
trigger:
  - platform: time_pattern
    seconds: /10
condition:
  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.genius_valve_21.last_updated) | int ) > 280 }}
action:
  - service: notify.persistent_notification
    data:
      title: Battery Check Failed
      message: >-
        Sensor last seen {{ now() - states.sensor.genius_valve_21.last_updated
        }}
mode: single

Do you realise this only changes if the state changes?

So if you receive 100%, 100%, 100%…etc as the state the last_updated property does not change.

Do all of these sensors use the same integration and, if they do, which one is it?

FWIW, some integrations have the ability to indicate an entity is unavailable if it has not received a new value within a desired time period. For example, the MQTT Sensor integration offers expire_after.

This is handy for detecting “dead” sensors.

That’s possible - but with temperature being one of the sensor metrics, in practice, it always jitters enough to send a reading… And the battery level is another metric, so that will generally decay, especially if we have a reading in /10ths of a %.

If sensor went for an hour without being noted, it’s a high probability that it’s died - at least enough to go and check manually.

It would be nice if there was a “last seen” property somewhere, but I gather there isn’t - so this’ll be OK…

Hi,

No - 3 integrations (so far):

ble_monitor from HACS (BLE fridge sensors)
z-wave (various devices)
genius (Heat Genius API integration to separate Genius Z-Wave hub that does the heating, on a separate ZWave network (many valves and thermostats with batteries).

I’ve tried this with all 3 and it looks like it will work as all 3 do normally report a state change quite frequently.

On a separate note, I was wondering if MQTT was the way out - but I wasn’t sure if it could be made to integrate with any random integration - and it seemed like more work :smiley:

It crossed my mind that if you’re using this to monitor a device’s battery level, is it necessary to poll it every 10 minutes? Batteries typically last a long time so checking their level once a day seems adequate.

Or have you selected a 10-minute interval to promptly detect “dead” sensors?

Hi - actually /10 was a 10 second testing for debugging.

In practise, I’d probably go for a one hour cycle to get a fairly prompt notification.