Just cannot get this automation trigger right

I’ve set up an input_select to pick from a short list of preset times, and put that into the “for” element of a state change. To me, the output looks exactly like it tells me it should look, but it clearly isnt! Here is my setup, and error in the log

Configuration:

input_select:
  snooze:
    name: "Snooze Timer (minutes)"
    options:
      - 10
      - 15
      - 20
      - 30
    icon: mdi:alarm-snooze

Automation:

- id: '1581548213506'
  alias: Stop Spotify on Bedroom Echo
  description: ''
  trigger:
    - entity_id: switch.snooze_timer
      for: '00:{{ states('input_select.snooze') }}:00'
      from: 'off'
      platform: state
      to: 'on'
  condition:
    - condition: state
      entity_id: switch.snooze_timer
      state: 'on'
  action:
    - entity_id: media_player.mark_s_2nd_echo_dot
      service: media_player.media_pause
    - entity_id: switch.snooze_timer
      service: switch.turn_off

Error Code:

2020-02-17 14:31:11 ERROR (MainThread) [homeassistant.components.automation.state] Error rendering 'Stop Spotify on Bedroom Echo' for template: offset minutes: '00:10:00' should be format 'HH:MM' or 'HH:MM:SS'

Any ideas would be greatly appreciated! I am sure its something simple but not being a programmer i struggle.

You need to use different types of quotes or the open and close pair you have is this:

for: '00:{{ states('

Try

for: "00:{{ states('input_select.snooze') }}:00"
1 Like

@tom_l is right.

I would also recommend changing it from “00:{{ template }}:00” to

for: 
  minutes: "{{ states('input_select.snooze') | int }}"

I’m pretty sure the way you have it would be a supported template as well, but they don’t give that as an example specifically. I just think it’s easier to read and you’ll be able to support any number > 99 minutes. I know I sometimes want to snooze for 100 minutes and I’d be super sad to know I could only do 99 minutes lol.

Another thing,


  trigger:
    - entity_id: switch.snooze_timer
      for: '00:{{ states('input_select.snooze') }}:00'
      from: 'off'
      platform: state
      to: 'on'
  condition:
    - condition: state
      entity_id: switch.snooze_timer
      state: 'on'

Your condition here is 100% doing nothing. The trigger already required switch.snooze_timer to be ‘on’, so the condition will be met 100% of the time.

I think I know why you put it which would make sense as it’s what I thought happened as well. You probably assumed this would trigger as soon as switch.snooze_timer turned to ‘on’, then would wait for X minutes before continuing. In this case, it would be wise to check “did the switch turn off in that amount of time?”

In reality, this will only trigger once it’s been ‘on’ for X minutes. There is no waiting. So the condition “is the switch.snooze_timer on?” will 100% of the time be true and can be removed.

3 Likes

Thanks for your help!

Hi Jim, Tom,

I’ve tried your suggestion (i had originally double quotes and it didn’t seem to like it)

i now get this error

2020-02-17 20:34:16 ERROR (MainThread) [homeassistant.components.automation.state] Error rendering 'Stop Spotify on Bedroom Echo' for template: offset minutes: "10" should be format 'HH:MM' or 'HH:MM:SS'

Essentially the same error but it seems to not like the double quotes… do i need to put quotes round my option in my input_select?

Hmmm, I found this:

for: 
  hours: 0
  minutes: "{{ states('input_select.snooze') | int }}"
  seconds: 0

If this doesn’t work, go back to how you had it and mark toms answer as the solution! (probably just mark his as the solution either way)

for: "00:{{ states('input_select.snooze') }}:00"

Its still showing essentially the same error:

2020-02-17 20:51:17 ERROR (MainThread) [homeassistant.components.automation.state] Error rendering 'Stop Spotify on Bedroom Echo' for template: offset "00:10:00" should be format 'HH:MM' or 'HH:MM:SS'

which is why i went to the single quotation marks in the first place… not ideal but now you are seeing my frustration! I’ve tried the HH:MM option too

could it be that input_select creates a text output and that i should be using input_number to get an integer? I’ll give it a go

yes, all states are strings. even if they look like numbers to you.

you need to convert the string to an int:

for: "00:{{ states('input_select.snooze') |int }}:00"

and you always have to use the opposite type of quotation marks outside your templates as you do on the inside of your templates.

if you think about how the jinja parser looks at the templates it makes sense.

if the parser sees:

for: '00:{{ states('input_select.snooze') }}:00'

it parses it as:

for: '00:{{ states('   input_select.snooze   ') }}:00'

compare that output to the correct one above and you’ll see the difference.

'00:{{ states('input_select.snooze') }}:00'
Yeah as Finity says it will see that as 2 strings - the quotes enclose each srting… That’s why you would use this instead:
"00:{{ states('input_select.snooze') }}:00"
or
'00:{{ states("input_select.snooze") }}:00'
OR you can even use 2 single quotes like this:
'00:{{ states(''input_select.snooze'') }}:00'

The automation GUI editor loves double single quotes.

Thanks both, i have cut and paste from above and now get this error:


2020-02-18 08:58:05 ERROR (MainThread) [homeassistant.components.automation.state] Error rendering 'Stop Spotify on Bedroom Echo' for template: offset "00:10:00" should be format 'HH:MM' or 'HH:MM:SS'

As i said, this is why i dropped the swapped from double to single in the first place.
Could i have something else wrong?

I copied your exact input_select and automation, changed the quotes in the ‘for’ option, template to seconds and the switch for testing. Works!

input_select:
  snooze:
    name: "Snooze Timer (seconds)"
    options:
      - 10
      - 15
      - 20
      - 30
    icon: mdi:alarm-snooze

automation:
  - id: '1581548213506'
    alias: Stop Spotify on Bedroom Echo
    description: ''
    trigger:
      - entity_id: switch.test_switch
        for: "00:00:{{ states('input_select.snooze') }}"
        from: 'off'
        platform: state
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.test_switch
        state: 'on'
    action:
      - entity_id: switch.test_switch
        service: switch.turn_off

There must be some other issue in your automations.

EDIT: Also works with the automation editor. This is what is left:

- id: '1581548213506'
  alias: Stop Spotify on Bedroom Echo
  description: ''
  trigger:
  - entity_id: switch.test_switch
    for: 00:00:{{ states('input_select.snooze') }}
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: switch.test_switch
    state: 'on'
  action:
  - entity_id: switch.test_switch
    service: switch.turn_off

The ‘for’ option without quotes around the template. :upside_down_face:

EDIT 2: @cavester, what HA version are you on?

1 Like

Sorry, am away on business so limited testing. @VDRainer your solution seemed to work in the short time i had before leaving, I’m on latest release 0.105.5 and i think supervisor 201 / i updated yesterday.

I’ll let you know more when i get home

I was using the front end tool but still putting in the double inverted commas, which is why i was getting the error, but not realising that was the cause!
I was in fact then applying double double inverted commas!

Thanks!