Int vs str in state

I’m setting up an automation for my son’s bedroom where if there is no button presses on his hue dimmer for the past 30 minutes, it will turn off the light.
I’m getting an error message, which I guess has something to do with whether the state is an integer or a string.
The error message reads:

File “”, line 1, in top-level template code
TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

I’ve based my code on this thread:

So here’s what I’ve put together:
The sensors

  - platform: time_date
    display_options:
      - 'date_time'
  - platform: template
    sensors:
      jakobdimmer_counter:
        value_template: '{% if states.sensor.jakobdimmer_updated.last_updated is undefined %}{{"00:00:00"}}{% else %}{{ ((as_timestamp(states.sensor.date__time.last_updated)-as_timestamp(states.sensor.jakobdimmer_updated.last_updated))|int) }}{% endif %}'
        entity_id:
          - sensor.date__time

And then the automation:

- id: "Jakob-lys autoav" # Skru av lyset til Jakob etter 30 minutters inaktivitet
  alias: "Jakoblys av på dagtid"
  trigger:
    platform: state
    entity_id: sensor.jakobdimmer_updated
  condition:
    condition: and
    conditions:
      - condition: time
        after: '08:00:00'
        before: '19:00:00'
  action:
    service: script.jakob_lys_autoav
```
Finally, the script:

jakob_lys_autoav:
sequence:
- wait_template: “{{ states.sensor.jakobdimmer_counter.state > 10 }}”
- service: light.turn_off
data:
entity_id: light.jakobs_rom
transition: 0

So what I can understand is that using > on a string doesn't work. How then do I tell the system that it is in fact an integer?

Try this:

"{{ states ('sensor.jakobdimmer_counter.state') | int > 10 }}"

4 Likes

That did it. Thanks!
Realized that the .state at the end was an error, too, so now it’s gone.

It turns out my counter only updates every minute. Is there any way to change this interval or even better, have it update whenever the source changes?

Not sure about that one, sorry.