Slider based turn off

Hi,

I want to have a Slider that turn a lamp on for the amount of time I set at this Slider but it does not work.
The example at the bottom of this page does not pass the config check.

This one does pass the config check but the lamp turns off right after it turns on. It ignores the value set with the slider:

- id: '1568651650894'
  alias: Lamp off
  trigger:
  - entity_id: switch.fritz_dect_200_2
    for: 00:{{ states.input_number.minutes | int }}:00
    platform: state
    to: 'on'
  action:
    entity_id: switch.fritz_dect_200_2
    service: switch.turn_off

Can someone help?

quotes for the for


    for: "00:{{ states.input_number.minutes | int }}:00"

I can’t recall if this field accepts templates either. @pnbruckner added it to a few fields, I think this was one place. So it should pass config check now, but you may want to verify if it works.

It passed the config check even without quotes. But it is not working.

Hah, didn’t even notice, you’re missing a bit of the template

    for: "00:{{ states.input_number.minutes.state | int }}:00"

Yes, thats it. Thanks alot! :slight_smile:
I don’t understand why the Input Number docu does not describe it that way or why the suggested way there is not working but at least its working now :smile:

well, it’s just how templates work. The templates docs and state object docs cover everything inside the {{ }}.

In the templates docs its suggested to avoid states.sensor.temperature.state and instead use states('sensor.temperature')
Why does for: ‘00:{{ states(‘input_number.minutes’) | int }}:00’ not pass the config check?

That’s because you have quotes inside quotes. The parser gets screwed up with that. So you need to use different quotes inside vrs outside:

"00:{{ states('input_number.minutes') | int }}:00"

or

'00:{{ states("input_number.minutes") | int }}:00'

or escape the quotes (only works with single quotes)

'00:{{ states(''input_number.minutes'') | int }}:00'

Ah, that makes sense. Thank you so much :blush:

Yep, as you discovered, this field does support templates. :slight_smile:

@Loader23, when the template was invalid or couldn’t be rendered, you should have gotten an error saying so. Did you? BTW, the validity of the template is not checked until run-time (unless there are basic YAML formatting issues, like incorrect/unbalanced quotes or something.)

BTW, if you pass just a number to the for: parameter it will be interpreted as seconds. So you could also do this:

for: "{{ states('input_number.minutes')|int * 60 }}"

Yes, I got this error with the incorrect quotes, but it didn’t help me to be honest :slight_smile:

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations.yaml", line 28, column 5
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 29, column 25

Wait what! So now I can change this:

  trigger:
    platform: template
    value_template: "{{ (as_timestamp(strptime(states('sensor.date_time'),'%Y-%m-%d, %H:%M')) - as_timestamp(states.switch.rumpus_dehumidifier.last_changed) ) / 3600 > states('input_number.rumpus_dehumidifier_run_time')|int }}"
  condition:
    condition: state
    entity_id: switch.rumpus_dehumidifier
    state: 'on'

to

  trigger:
    platform: state
    entity_id: switch.rumpus_dehumidifier
    to: on
    for:
      hours: "{{ states('input_number.rumpus_dehumidifier_run_time')|int }}"

This is brilliant.

1 Like