Sending a sensor as a variable to a script

Hi there,

I have multiple battery powered sensors. Sometimes the battery runs out, and this can be problematic. I wanted to create something that notifies me when the batteries are dead / the sensor is not updating.

My thought was to create an automation that calls a script, the script checks against a timeout variable to see if the sensor has updated in the last 24 hours, if not it will send a notification.

The problem is that i cannot find the right way of passing the sensor as a variable. My best guess would be to do it as follows:

Automation:

alias: "Global: Notify when sensors appear low on battery"
description: ""
trigger: []
condition: []
action:
  - service: script.notifysensorlastchangedscript
    data:
      sensorId: sensor.washingroomtemperaturehumidity_temperature
      sensorDescription: temperatuur sensor in het washok
mode: single

Script:

alias: NotifySensorLastChangedScript
fields:
  sensorId:
    name: Sensor id
    description: The id of the sensor for which to check and send a notification
    example: sensor.washingroomtemperaturehumidity_temperature
    required: true
  sensorDescription:
    name: Sensor description
    description: Description of the sensor, this will be used in the message.
    example: temperatuur sensor in het washok
    required: true
variables:
  sensorLastChanged: states.{{ sensorId }}.last_changed
sequence:
  - if:
      - condition: template
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(sensorLastChanged)) >
          states.input_number.globallastupdatedtimeoutseconds.state | float}} 
        alias: Check if changed within allowed limit
    then:
      - service: notify.bothusers
        alias: Send a notification
        data:
          data:
            clickAction: lovelace/washing-room
          title: Batterij leeg
          message: >-
            De {{ sensorDescription }} heeft al sinds
            {{states.sensor.washingroomtemperaturehumidity_temperature.last_changed
            }} geen waarde doorgegeven. Batterij vervangen? 
mode: single
icon: mdi:alarm-note


The problem is that after running the script I get the following error message;

Error: In ‘template’ condition: ValueError: Template error: as_timestamp got invalid input ‘states.sensor.washingroomtemperaturehumidity_temperature.last_changed’ when rendering template ‘{{ (as_timestamp(now()) - as_timestamp(sensorLastChanged)) > states.input_number.globallastupdatedtimeoutseconds.state | float}}’ but no default was specified

So it seems like the value did come through correctly but it has added brackets, so I think i somehow have to dereference or make it understand that its not a string but i dont know how. When se tthe sensorLastChanges variable to the same value it works.

Any ideas on how to fix this? Or how to do this in a better way? thanks in advance!

This is a string but a datetime is required. You need to template the sensor state.

The following is working for me… I tried a variety of methods to get sensorLastChanged to work in the condition of the if, but none of them would work.

alias: NotifySensorLastChangedScript
fields:
  sensorId:
    name: Sensor id
    description: The id of the sensor for which to check and send a notification
    example: sensor.washingroomtemperaturehumidity_temperature
    required: true
  sensorDescription:
    name: Sensor description
    description: Description of the sensor, this will be used in the message.
    example: temperatuur sensor in het washok
    required: true
variables:
  sensorLastChanged: "{{ states[sensorId].last_changed | as_local }}"
  timeout: "{{ states('input_number.globallastupdatedtimeoutseconds') | float(0) }}"
sequence:
  - if:
      - condition: template
        value_template: |
          {{ now() - states[sensorId].last_changed | as_local 
          > timedelta( seconds = timeout) }}
        alias: Check if changed within allowed limit
    then:
      - service: notify.bothusers
        alias: Send a notification
        data:
          data:
            clickAction: lovelace/washing-room
          title: Batterij leeg
          message: |
            De {{ sensorDescription }} heeft al sinds
            {{ sensorLastChanged }} geen waarde doorgegeven. Batterij vervangen? 
mode: single
icon: mdi:alarm-note
1 Like

Thanks @ Didgeridrew this works perfectly and does exactyl what I want!