MQTT triggers are OK - although they’re more complicatecd than state triggers so in my opinion should only be used where a state trigger is not available, but that’s personal choice. The problem is the DEVICE triggers and actions. Here’s why…
(This is going to be quite a long post, but explains it the best I can, so try and stick with it )
Imagine this for a simple scenario:
- You have a downstairs in a house with two rooms - living room and kitchen.
- Each has a zigbee light in them - light.livingroom and light.kitchen
- You also create a light group called light.downstairs that conrols both the livingroom and kitchen light together.
So you buy a hue 4 button remote and position in the doorway that joins the two rooms. The hue 4 button remote has three events for each button - single_click, long_click and double_click.
You decide to automate as follows:
- single clicks will control the livingroom
- double clicks will control the kitchen
- long clicks will control both
- the on button will turn the appropriate light on
- the off button will turn the appropriate light off
- the brightness up button will increase the brightness of the appropriate light by 10%
- the brightness down button will decrease the brightness of the appropriate light by 10%
Using device discovery, your hue 4 button remote has created a sensor called sensor.light_switch
So, using the simple state based triggers and actions (which support templating) you can create a fairly simple single automation to do all of the above
(Psuedo code to avoid writing the templates, and making this post even longer!)
alias: control all downstairs lighting
trigger:
platform: state
entity_id: sensor.light_switch
action:
- choose:
- conditions: >
# template to define if this was an on or off button that was pressed
sequence:
- service: "light.turn_{{ trigger.to_state.state|replace('-press', '') }}"
entity_id: >
# template to define if this was a single_click, long_click or double_click
# and output livingroom, kitchen or downstairs as apprpriate
default:
# this means one of the brightness buttons has been pressed
- service: light.turn_on
data:
entity_id: >
# template to define if it was a single_click, long_click or double_click
# and output livingroom, kitchen or downstairs as apprpriate
brightness_step_pct: "{{ 10 if trigger.to_state.state == 'up-press' else -10 }}"
Now to do this with device triggers and actions, you would need 12 automations (or a very complicated single automation with 12 triggers and 12 actions) due to the inability to template. And crucially each automation will contain the device_id for the trigger and a device_id for the lights in the actions. This is where the problems start.
Scenario 1 - minor failure
Imagine that the hue 4 button remote falls off the wall and smashes into a thousand pieces so has to be replaced. When it is replaced you can set the entity_id for the replacement to the same entity_id that you had before. 20 seconds work, and the automation I gave above continues to work.
BUT the device_id will have changed because homeassistant sees it as a new device, so if you used the device triggers you now have to go through 12 automations and change the device id in all your triggers. This wouldn’t be quite so bad if device_ids were easy to discern, but they’re very long ‘random’ things and frankly it’s a pain in the backside.
Scenario 2 - big failure
Imagine that your zigbee dongle fails. You purchase a new dongle and set about re-pairing your bulbs and your hue remote. Again, you can quickly set the entity_ids back to what they were before, but homeassistant sees these all as new devices and assigns them all new device_ids.
Again, using my state based automation above, there is no further work to do. The entity_ids match again, so the automation works the same as it did before.
But for device based automations, you now have to go through and replace 12 device_ids in the triggers and 12 device_ids in the actions.
Somewhere in this process it is likely a mistake is going to be made and you’re going to end up in a world of problems.
All of these problems have been caused by only 3 entities/devices. When you multiply this by how many entities/devices you actually have in your house, using device triggers and actions is a recipe for a headache.
I have around 40 zigbee devices in my house currently and intend to add more. The idea of using device triggers and actions to control them sends shivers down my spine!
Hope this makes sense.