Trigger event when state is below a threshold for some time not working

This gives me an error

16-05-31 22:31:47 homeassistant.bootstrap: Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->trigger->0->for. (See /config/automation.yaml:348)
- alias: "TV off when off"
  trigger:
    platform: numeric_state
    entity_id: sensor.TV_watts
    below: 20
    for:
       minutes: 1
  action:
    service: switch.turn_off
    entity_id: switch.TV

Any idea why?

indentation is wrong (3 spaces instead of 2) and numeric_state doesn’t work with for, you could work around by adding a condition or delay in your script …

Thanks.how can this be done in a condition? I as thinking a script but that seems overly complicated.

if you want to get anything done right, it will be complicated, last time I checked my conf has over 8000 lines of code.
I feel for you as a lot is not straightforward but I prefer the coders advancing the project than the documentation…

in your case you’ll need to investigate templating with a state trigger, you can put a ā€˜{{ states.sensor.tv_watts.state < 20 and trigger.for > 60 and states.switch.tv == ā€œonā€ }}’ in a template condition

1 Like

@thefrenchmatt thanks I’ll give that a try.

Ok. here we go:

- alias: "TV off when off"
  trigger:
    platform: numeric_state
    entity_id: sensor.TV_watts
    below: 20
  condition: 
    condition: or
    conditions:  
    - condition: template
      value_template: '{{ states.sensor.tv_watts.state < 20 and trigger.for > 60 and states.switch.tv == "on" }}'
  action:
    service: switch.turn_off
    entity_id: switch.TV

however the TV does not turn off after 1 minute. I suppose the trigger.for > 60 means 60 seconds right?

Sorry for not answering sooner, spent the last 3 days moving flats and still plenty to fix in my config :wink:

I think times are in seconds in ā€œtrigger.forā€ but I’m not sure as I’m using (ā€œto: state_valueā€ together with ā€œfor: seconds:ā€) in my trigger (here you could tweak the ā€œupdate_intervalā€ in your sensor.tv_watts config but I’m not sure of the effect nor default values so YMMV).

As you made a nice effort, here’s how I’d do it:

- alias: "TV off when off"
  trigger:
    platform: state
    entity_id: sensor.TV_watts
  condition: 
    - condition: template
      value_template: '{{ states.sensor.tv_watts.state < 20 and trigger.for > 60 and states(switch.tv) == "on" }}'
  action:
    service: switch.turn_off
    data:
      entity_id: switch.TV

@thefrenchmatt thanks. I tried but it still doesn’t work. Its also very difficult to debug. It simply does not turn off the TV although the wattage is below 20…

Why the different syntax: states.sensor.tv_watts.state < 20 but states(switch.tv)=="on" - why? shouldn’t it be the same syntax?

haven’t quite figured it out myself, wiki says one syntax will return an error and another doesn’t, but I’m also lost on how to debug the flows properly :confused:
one thing that worked for me is using |round(0) after state values, sometimes hass doesn’t interpret the states in the correct format and the jinja2 wiki + stackoverflow are full of answers that may fit your case…
try value_template: '{{ ( states(ā€œsensor.tv_wattsā€)|round(0) < ā€œ20ā€|round(0) ) }}
then call a script instead of switch_off, you can then add a delay in your script, you’ll then need another automation to cancel your switch_off script in case the watts go above 20…