Action on failed trigger

This is a tricky one. I have this semi-complex automation which basically looks for motion and turns the lights on for 1m if it detects it after sunset.

The complexity arises in the smarts however. I have modified it such that any additional trigger of motion extends the lights by resetting the countdown. In order to do this properly, I create a scene of the light’s state prior to it being turned out. This stores things I muck with such as brightness for example and restores it to the default for when the switch will manually be turned on again. So far so good that’s not terribly difficult.

But now I want to make it such that the lights never turns off automatically when the user hits the switch up. In essence, it’s my way to tell the automation to not reset things.

The weird thing is that I’m using a Innovelli light and it looks like the only signal I could detect of a use pressing up is a “Press - Up” event that is fired. The state doesn’t seem to change at all (unless I press & hold the switch and affect the brightness). Because it’s an event, it seems like I need to use triggers to achieve this.

The natural way to think about it for me was to replace the 1m timer with a wait trigger and to execute the rest of the automation only if the trigger timed out. However It doesn’t seem like the action was designed that way. It seems to only allow me to go through if the trigger happened OR (optionally) if the timeout happened.

You could argue that I could rely on multiple automation scripts to do this, capture the event and cancel the parallel automation that was previously kicked off to automate the lights, but that tends to be relatively ugly especially since I’d like to blueprint this so I can pply it to many lights. I don’t mind executing lambdas if there’s a clever programmatic way to achieve this either.

My other thought was to look at the time the entity was last updated, but it seems like the trigger doesn’t actually change the entity and thus that timestamp doesn’t change. Maybe there’s a way to look at logs and see when was the last time any event fired on that entity? In which case, I could use an “if/then/else” construction to see if the entity was modified since the creation of the temporary scene I created as a restoration point after the automation?

Here’s what I’m working with right now:

alias: Living Room
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.dev_ttyama0_living_room_motion
    to: "on"
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
action:
  - if:
      - condition: state
        entity_id: light.living_room_light
        state: "off"
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: temp_living_led
          snapshot_entities:
            - light.living_room_light
      - service: light.turn_on
        data:
          brightness_pct: 20
        target:
          entity_id: light.living_room_light
  - if:
      - condition: and
        conditions:
          - condition: template
            value_template: "{{ states.scene.temp_living_led.state is defined }}"
    then:
      - delay:
          hours: 0
          minutes: 1
          seconds: 0
          milliseconds: 0
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.temp_living_led
      - service: scene.delete
        metadata: {}
        data: {}
        target:
          entity_id: scene.temp_living_led
mode: restart

Hmm The video on that page did the trick. In the end, the solution was to have multiple conditions with IDs and have a conditional to pick which of the 2 triggers happened. If it was done by the trigger to cancel the ‘reset’, then just clean off the states.

1 Like