Condition issues (I think)

Hello.

I have a small problem that I would appreciate your input on.

What I am trying to achieve is the following:
When the TV is turned off, I want to turn off some lights if a motion sensor hasn’t registered any movement for x minutes. If there has been movement within the threshold, the automation should wait until the movement has been off for x minutes and then trigger the action. Another thing to take into account is that the motion sensor might have been ‘off’ for a while when the TV is switched off, so I should have to wait a little bit before checking the state of the sensor.

The trigger could therefore look something like this:

trigger:
  - platform: state
    entity_id: input_select.harmony
    to: 'PowerOff'
    for:
      minutes: 2

I have found quite a few examples using the motion sensor as the trigger, but I want to use the TV as the trigger and so far I’ve been SOL on this issue. All help and hints are greatly appreciated!

you can have multiple triggers in an automation, that’s how i’ve done it.
I personally don’t use the TV -> Off to switch my lights off, I only use my PIRs as this truly mean there’s nobody in the room, but I have a condition that the TV should be off (so I don’t switch the lights off if I’m not moving because I’m watching the TV)
I actually dim my lights if there’s no movement, in case I’m lazying on the sofa with the TV off, then I can wave and the lights come back on, instead of being plunged in darkness :slight_smile:
See if my automation helps you:
(I know I’m calling the light off twice, but my LightWaveRF switch is old and doesn’t always respond the first time)

- alias: Living Room Main Light Off PIR
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_living_room_front
      to: 'off'
    - platform: state
      entity_id: binary_sensor.pir_living_room_back
      to: 'off'
      for:
        minutes: 2
  condition:
    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.living_room_main_light_dimmed_off_pir.attributes.last_triggered | default(0)) | int > 120 )}}'
    - condition: state
      entity_id: input_boolean.normal
      state: 'on'
    - condition: state
      entity_id: media_player.panasonic_viera_tv
      state: 'off'
    - condition: state
      entity_id: binary_sensor.pir_living_room_front
      state: 'off'
    - condition: state
      entity_id: binary_sensor.pir_living_room_back
      state: 'off'
    - condition: state
      entity_id: light.living_room_main
      state: 'on'
  action:
  - service: light.turn_off
    data:
      entity_id: light.living_room_main
  - service: light.turn_off
    data:
      entity_id: light.living_room_main

More here:

Hello and thank you for replying so fast!

Yes, I know I can use multiple triggers, but whenever one of the triggers fires, the rule is executed. So I can’t see that this would be helpful in my case since turning off the TV should be the sole trigger :slight_smile:

But I might have to rethink this and do it as you’ve done, to use the sensor as the trigger.
Brainwaves triggering!! :nerd_face:

1 Like

You can have multiple conditions so even if one trigger goes off, if the other triggers are also conditions, they will all need to be met… So it doesn’t matter if one trigger goes off, the conditions must also be met. (And you can use and/or in conditions as well)

That should work fine if you add a wait_template (Script Syntax - Home Assistant)

So basically you use the trigger that the TV is powered off for 2 mins. Then you ask the wait_template to wait until the motion sensor is off for a specific amount of time. As soon as this happens, it will trigger as true.

@lolouk44’s solution is correct, ignore the fact that his automation has more than one trigger (That has nothing to do with your problem). This is the condition you want to emulate with your motion sensor:

  condition:
    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.living_room_main_light_dimmed_off_pir.attributes.last_triggered | default(0)) | int > 120 )}}'

You just need to verify that the state hasn’t changed (which that condition does). And you need to verify the state.

it would look something like this:

  condition:
    - condition: template
      value_template: >
        {{ is_state('binary_sensor.motion','off') 
           and (as_timestamp(now()) - as_timestamp(states.binary_sensor.motion.attributes.last_triggered | default(0)) | int > 120 ) }}
2 Likes

Hello again.
I am still struggling with this automation. These are the “schematics”:

  • Turn off TV
  • Motion sensor by TV get triggered when turning off TV and walking out of the room
  • Motion sensor in living room gets triggered as someone walk downstairs.
  • The two motion sensors should then be in “off”-state for at least ten minutes before turning off certain lights.

Only way I think I can get this to work, is by using a wait_template. My config now looks like this:

- alias: Set Nightmode
  hide_entity: True
  trigger:
    - platform: state
      entity_id: input_select.harmony
      to: 'PowerOff'
      for:
        minutes: 1
  condition:
    - condition: time
      after: '23:00:00'
      before: '05:00:00'
   action:
    - wait_template: >
        {{ is_state('sensor.sensor_motion_stue','off')
          and (as_timestamp(utcnow()) - as_timestamp(states.sensor.sensor_motion_stue.last_updated) | int > 120) }}
    - service: notify.android
      data:
        message: Test

When I check my template by using the Template editor in HA, it gives the correct true or false depending on the states. If it’s true, the automation triggers the message. But, if the time or the motion gives a false, it will wait (it seems like), but after the given time, nothing happens. Ie. the next action service is not triggered. Is anyone able to see what I am doing wrong?

Why even involve the TV in the trigger? Use the motion sensors being ‘off’ for 10 minutes as the trigger and then check the state of the TV as your condition.

Yes, that is what I’ve thought of as well, but then I would get issues when someone eg. reads a book without the TV being switched on. I want to avoid using the sensor as the trigger to avoid these types of false positives.

Then you’ll have to move to a stronger logic than what you have above. I suggest moving to AppDeamon or writing your own component to handle this situation.

yep, thats the way I found it to work best, and not have to write out all possible scenarios…

using the Hue motion sensors (which handle the response to motion, and can be set with an variable sensitivity for motion and light), I have them on in the house, and have several action modes switch them Off. (my translation of your conditional value_template on the Tv)

In human language: Hometheater Mode ? switch off Home theater sensors. Needs a bit of human intelligence to be able to also use that when reading a book :wink:

have several Home Modes (Guest, Away, etc) to switch all sensors On/Off, just for ease of use.