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:
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?
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 }}
#
template:
- trigger:
- platform: state
entity_id: light.den_shelly_dimmer_2
sensor:
- 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' %}
n/a
{% elif c_id != none and c_parent == none and c_user == none %}
physical
{% elif c_id != none and c_parent == none and c_user != none %}
dashboard_ui
{% elif c_id != none and c_parent != none and c_user == none %}
automation
{% else %}
unknown
{% endif %}
unique_id: den_dimmer_light_off_context
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 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: