Distinct Events for "Switch Turned Off" vs "Switch Off event was fired" -- How to Link Automations?

I have a Zigbee rocker switch, and I’m trying to build an automation with it to control some Zigbee lamps.

The problem is that there are two events that can be sent from the switch. The first is “Switch Turned Off”, which is only sent if the switch “on” button was pressed. The second is “Switch off event”, which is sent if the switch already think it’s in an off state.

The problem I have is that if the lamps are turned on another way (e.g. from HA app), but then you try to hit the off button on the switch, it is firing the “Switch off event” instead of the “Switch Turned Off” (since the on button was never pressed). But the automation I have only triggers for “Switch Turned Off”, and I don’t know how to make “Switch off event” a trigger in the automation.

Any advice on how to do this? Below is an example of these two events in the logbook:


Not sure if this helps but:

  1. I am using alot of Shelly relays and they have the concept of a switch being set as “edge” in which case you ignore whether the switch is physically turns on or not, and the next time the switch it changed (turned on or off) then whatever the lights were, is changed to the other state. This allows for my situation where the physical switch and remote control of same can be done seamlessly (removed the issue of someone having to toggle a switch off and back on to turn it on for example). Are you able to do this with your situation?

  2. I have a bunch of sensors I use to let me know when something is turned off, how it was turned off (so I can use that in my code to not turn a light back on when someone has recently physically turned it off and then the motion sensor notices them walking out of the room for example). An example of the configuration.yaml code is below for keeping track of that context. Note, in the condition section of an automation the same trigger properties can be used as well (but I thought it was “cleaner” to keep that separate and in case I want to use the same information for any other logic as well):

# Den Dimmer (for accessing these and with the last_changed value):
# {{ states('sensor.den_dimmer_light_off_context') }}
# {{ states.sensor['sensor.den_dimmer_light_off_context'].last_changed }}
  - trigger:
      - platform: state
        entity_id: light.den_shelly_dimmer_2
      - name: "Den Dimmer Light Off Context"
        state: >
          {% set c_id = trigger.to_state.context.id %}
          {% set c_parent = trigger.to_state.context.parent_id %}
          {% set c_user = trigger.to_state.context.user_id %}
          {% if states('light.den_shelly_dimmer_2') == 'on' %}
          {% elif c_id != none and c_parent == none and c_user == none %}
          {% elif c_id != none and c_parent == none and c_user != none %}
          {% elif c_id != none and c_parent != none and c_user == none %}
          {% else %}
          {% endif %}
        unique_id: den_dimmer_light_off_context

Does that help at all?

Thanks for the reply.

I don’t think those help, because that’s not quite my situation. I think it’s just that the switch itself sends different events based on an internal state (and that state has nothing to do with home assistant).

I do wonder if switching from ZHA to Z2M is the right choice; my understanding is that there is a lot more control over details and events in Z2M…

I am a big shelly believer (native integration, works offline, no hub required, etc., etc.)

I think there is no way, using the UI editor, to use the event that I wanted. The UI editor uses the reported state of the switch, but I want to use the “on” or “off” events regardless of the switch state.

Here’s how I solved the issue.

Go to Developer > Events, then type “zha_event” and click start listening. Then I push the button on the switch, and get an output that looks like this:

event_type: zha_event
  device_ieee: ZZZ
  unique_id: YYY
  device_id: XXX
  endpoint_id: 2
  cluster_id: 6
  command: "off"
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-09-08T20:20:26.216163+00:00"
  id: 01H9V77XZ8J2CV1V86VBW55D5B
  parent_id: null
  user_id: null

Then, I went to the automation I had, and under the trigger I chose to edit it in YAML. I then put it in the following YAML for the trigger:

platform: event
event_type: zha_event
  device_ieee: ZZZ
  unique_id: YYY
  device_id: XXX
  command: "off"

Now I can use the events from the switch for automations, regardless of the “state” of the switch.

1 Like