Help with template - Not playing for X minutes

Hi all,

I was hoping for some help with an automation.
I have the following condition working fine. But I want the minutes to work from an input_number.

What I have so far:

- alias: '[Occupancy] Lounge cleared by motion'
  trigger:
    platform: state
    entity_id: binary_sensor.lounge_pir
    to: 'off'
    for:
      minutes: "{{ states('input_number.occupancy_timeout') | int(20) }}"

  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: media_player.lounge_kodi
        state: 'playing'
        for:
          minutes: 20
      - condition: state
        entity_id: media_player.lounge_echo
        state: 'playing'
        for:
          minutes: 20

  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.lounge_occupancy_switch

I am assuming I need a template condition but I am a bit stuck.

Hopefully someone can point me in the right direction :slight_smile:

I doubt you can template that. So you’ll need something like a timer to trigger a second automation that would test if the two devices are still playing or not. The first automation then would set the timer and the timer duration can be templated.

Only for triggers support templates. Conditions do not. Please post your full automation, maybe there’s other ways to get what you want without templating. Worse case is that a template is built, it wouldn’t be too bad and you don’t need a timer.

Thanks. I have edited OP to include full automation.

I have ‘occupancy switches’ and need to turn this one off when:

  • no motion for x minutes
  • tv not been playing for x minutes
  • music not been playing for x minutes

It’s a bit painful but you can expand it if you have more media_players in the future. Just add them to the list with states.<entity_id>, .

The general idea here is:

  • Get the timeout duration.
  • Get the current time minus the timeout duration, convert to seconds.
  • Look through all the media players, if they aren’t playing, make sure they haven’t changed over the timeout duration.
  • If none have changed in the timeout duration, the condition passes.
condition: template
value_template: >
  {%- set players = [
    states.media_player.lounge_kodi,
    states.media_player.lounge_echo,
    ] %}
  {%- set timeout = states('input_number.media_timeout') | int(20) %}
  {%- set t = now().timestamp() - timeout * 60 %}
  {%- set ns = namespace(not_playing=[]) %}
  {%- for player in players %}
    {%- if player.state != 'playing' and as_timestamp(player.last_changed) < t %}
      {% set ns.not_playing = ns.not_playing + [ player.entity_id ] %}
    {%- endif %}
  {%- endfor %}
  {{ ns.not_playing | length == players | length }}

You should test it in the template editor because I have not tested it.

THANK YOU!!!
It would appear you are a wizard lol.

I had to change
{{ ns.not_playing | length == players.length }}
to
{{ ns.not_playing | length == players | length }}

It’s a nice elegant solution and easy to follow so thank you very much!

1 Like