How to use template sensor + "for:" as condition

Trying to figure this out. I have an automation that relies on the condition
{{ not is_state('media_player.man_cave_roku_2', 'playing') }} in order words, once the roku goes from ‘playing’ to anything else (idle, paused, etc) then the lights turn off. However, I’d like it to only trip after this has changed for 10 minutes.
Any ideas how I’d do this?

Thanks!

Do it in the automation triggered by the template sensor.

Well the trigger is actually something different - it’s triggered based on the motion sensor and there are multiple conditions which are variations on the condition, so I can’t change it around.

Try:

value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('media_player.man_cave_roku_2', 'last_changed') | default(0)) | int > 600)}}"

This will only tell you that the media player last changed state greater than 10 minutes ago, you will still also need to test for ‘not playing’.

it’s usually easiest to tell what you are trying to accomplish if you post your complete automation as you have it now.

1 Like

Good call. Here’s what I have so far. Currently it triggers after the motion detector shuts off & the two media players aren’t playing.
I’d like it to wait for the media players to be inactive for at least 10 minutes.

- id: '1508624466805'
  alias: Basement Light Off
  trigger:
  - entity_id: binary_sensor.basement_motion_44
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: template
    value_template: '{{ not is_state(''media_player.man_cave_roku_2'', ''playing'')
      }}'
  - condition: template
    value_template: '{{ not is_state(''media_player.justin_s_ipad'', ''playing'')
      }}'
  - condition: state
    entity_id: binary_sensor.vistacam_700_motion_sensor_18
    state: 'off'
  action:
  - data:
      entity_id: light.basement_52
    service: light.turn_off
 initial_state: true

You triggers wont work as you expect. Say you switch the players off and go upstairs, the motion sensor goes off but the players have not been off for ten minutes. Ten minutes later what is going to trigger the automation?

One possible way:

- id: '1508624466805'
  alias: Basement Light Off
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.basement_motion_44
    to: 'off'
    for: 
     minutes: 15
  condition:
  - condition: template
    value_template: "{{ not is_state('media_player.man_cave_roku_2', 'playing') }}"
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('media_player.man_cave_roku_2', 'last_changed') | default(0)) | int > 600) }}"
  - condition: template
    value_template: "{{ not is_state('media_player.justin_s_ipad', 'playing') }}"
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('media_player.justin_s_ipad', 'last_changed') | default(0)) | int > 600) }}"
  - condition: state
    entity_id: binary_sensor.vistacam_700_motion_sensor_18
    state: 'off'
  action:
  - service: light.turn_off:
      entity_id: light.basement_52

There’s a bit of an extra delay (15 minutes instead of 10) but it should always work.

Maybe this was wishful thinking, but I thought that once the motion detector triggered things off, it would basically “stall” until one of the conditions was met. Is that incorrect?

EDIT: Wow I was way off. ^^ Just tried a simple automation with a couple light switches and, yeah, if the condition isn’t true immediately, it doesn’t wait for it to be true. I have no idea how all my automations work so well, but now I understand why they fail when they do!

Can you explain the nested conditions you have here? Shouldn’t there be something like
condition: and in there at some point? Because as I read it now… it says “if any of the 4 following conditions are true”, right? (And in this case a video playing for > 600 seconds would trigger as true even though the state is ‘playing’, right?) I’m still wrapping my head around this stuff, so please don’t interpret this as prying or criticism… just me trying to understand something complex! I appreciate your guidance.

Can you explain the nested conditions you have here? Shouldn’t there be something like
condition: and in there at some point? Because as I read it now… it says “if any of the 4 following conditions are true”, right? (And in this case a video playing for > 600 seconds would trigger as true even though the state is ‘playing’, right?) I’m still wrapping my head around this stuff, so please don’t interpret this as prying or criticism… just me trying to understand something complex! I appreciate your guidance.

Conditions are AND by default. All five conditions have to be true for the action to be performed.

Conditions dont trigger automations. Only triggers do. Conditions are applied after the trigger happens. Also there is a condition to check for NOT playing which is ANDed with this time condition.

So only when the motion_44 sensor has been off for 15 minutes will the automation trigger. Then all five conditions are checked and if they are all true the action is performed.

EDIT: Just saw your edit. Yeah automations dont wait for conditions to be true. Trigger → check conditions → if true do actions, else exit.

Cool. That really opens my eyes.

So conditions are AND by default and triggers are ORs? (So multiple triggers can exist but only one has to be positive to start the automation?)

Correct.

Another way to do this would be to trigger every minute and check the conditions but that’s pretty wasteful of CPU resources and as it’s not really a time critical automation the 15 minute motion off trigger should do.

Now I feel like I need to print out all of my automations and get out my logic decoder ring :wink:

Just to muddy the waters a bit more you can have ORed conditions but you have to explicitly state that in your config. Check the Docs.

1 Like

Just was thinking about this and I might end up making a separate Automation that is triggered by the media devices which will turn the original automation off (and another for on). It means another automation but would be cleaner looking and less prone to errors, I’d think.