Capture Source of Event

Hello,

I’m trying to use the physical press on/off of a z-wave switch to control a group of z-wave outlets.

The light connected to the z-wave switch is automated by the sunrise/sunset so I don’t actually want to change it’s state when the switch is pressed just the state of the group of z-wave outlets.

I’ve done this in the past with SmartThings by listening for the on/off event then if it’s a physical button press turning the group of lamps on/off and then switching the light connected to the switch back to it’s original state (an infinite loop is prevented by only triggering when it’s a physical switch press not virtual otherwise turning the light back to it’s original state will trigger another event).

Is there a way to tell when a state trigger is from the physical switch rather than virtual event triggered by HA?

I’ve also tried to prevent this loop using a condition with a “trigger.for > 2” to see if the previous state was set for more than 2 seconds preventing a loop but I can’t seem to get that working either.

automation:
  - alias: 'Sunset Outside Lights On'
    trigger:
      - platform: sun
        event: 'sunset'
    action:
      service: switch.turn_on
      entity_id: group.outside_lights
  - alias: 'Sunrise Outside Lights Off'
    trigger:
      - platform: sun
        event: 'sunrise'
        #offset: '-00:45:00' 
    action:
      service: switch.turn_off
      entity_id: group.outside_lights
  - alias: Control Living Room Lights On
    trigger:
      - platform: state
        entity_id: switch.ge_45609_onoff_relay_switch_switch_2
        from: 'off'
        to: 'on'
    condition:
      - condition: template
        value_template: '{{ int(trigger.for) > 2 }}'
    action:
      - service: switch.turn_on
        entity_id: group.living_room
      #- service: switch.turn_off
      #  entity_id: switch.ge_45609_onoff_relay_switch_switch_2
  - alias: Control Living Room Lights Off
    trigger:
      - platform: state
        entity_id: switch.ge_45609_onoff_relay_switch_switch_2
        from: 'on'
        to: 'off'
    condition:
      - condition: template
        value_template: '{{ int(trigger.for) > 2 }}'
    action:
      - service: switch.turn_off
        entity_id: group.living_room
      #- service: switch.turn_on
      #  entity_id: switch.ge_45609_onoff_relay_switch_switch_2

Thanks!

1 Like

Have a look at the documentation for what data is available inside the trigger value https://home-assistant.io/getting-started/automation-templating/#available-trigger-data

Sorry but the documentation on trigger.for is not helpful. I posted here before guessing it was seconds (as it worked fine for a while). But now - being a timedelta - I cannot understand how to use it in a template, and of course cannot check myself with help of the templating section. Please @balloob, can you let us know how to format it correctly to seconds?

And now revisiting my reply 4 months later, I see I’d better give a try to also add to the wiki. One of my recent post about macros & loops contains an answer to using the trigger.for …

You’ll need to split the variable using the space! .split(’ ') and then have a dict you can access with [0] and [1].

Then you have 2 strings, so make the first an |int and the other part will be seconds, minutes, hours, days.

Probably not the granularity needed but I can share a way to do it with timestamps.

am running into the same question. i can’t follow your answer however… i understand split conceptually, but i can’t see what it’s giong to split into so i cannot figure out how to parse it. can you include an example of what you did? thanks!

If you do a split on an empty space, then your first element is an integer representing the duration, the second element the unit (seconds, minutes, hours, days).