Using a counter value in template


I am trying to build a script which counts the number of minutes a sensor has been open for, and then sends this using a script I use to send iMessages and then tts the same info:

  alias: Loop Front Gate Open 1
    - condition: state
      entity_id: binary_sensor.front_gate
      state: 'on'
    - delay:
        seconds: 5
    - service: counter.increment
      entity_id: counter.front_gate_open_time

    - service: shell_command.send_imessage
        entity_id: counter.front_gate_open_time
        message: 'INFO - THE FRONT GATE HAS BEEN OPEN FOR {{ states("counter.front_gate_open_time") }} minutes.'

    - service: script.turn_on
      entity_id: script.loop_front_gate_open_2

    - condition: state
      entity_id: input_boolean.notify_tts_info_alerts
      state: 'on'

    - service: tts.macos_say
        - media_player.hallway
        - media_player.gym
        - media_player.macmini
        message: 'Warning - the front gate has been open for {{ states("counter.front_gate_open_time") }} minutes.'

In the log I get:

2018-12-30 18:57:46 ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: ‘counter’ is undefined

Any help much appreciated! :slight_smile:

Ignore me!

I changed to: {{ states.counter.front_gate_open_time.state }} BUT was forgetting to reload-scripts facepalm was instead basing the reload-automations button and wondering why nothing was changing!

The above works, if a little ham fisted I’m happy with what it does :slight_smile:

use states( ’ . ’ ) not states.domain.entity because the latter is not good practice and will in future be deprecated . This means in future you might find it difficult to find the cause of an error that will be thrown in future.

1 Like