Last week I received Aqara magic dice in post and have been experimenting a bit with it.
It was tad harder to setup than I expected (for some reason did not see ‘side’ property in condition parameters) and as such I wish to share here my automation and also inquire as ‘how it could be improved’ by those who understand HA automation templates / platforms better than I do.
As I did not understand too much of different ways of how to go about doing this I ended up with using ‘mqtt’ platform which seems to offer rather low level access/reactions to something zigbee2mqtt is providing to Home Assistant.
After digging around docs/this community and testing and trying to understand logs I finally got conditionals working after bit (or a lot) of experimenting.
alias: dice controls
description: ""
trigger:
- platform: mqtt
topic: zigbee2mqtt/mydice1
condition:
- condition: template
value_template: |-
{{
trigger.payload_json.action == 'slide'
or trigger.payload_json.action == 'tap'
or trigger.payload_json.action == 'rotate_left'
or trigger.payload_json.action == 'rotate_right'
}}
action:
- service_template: |-
{% if trigger.payload_json.action == 'tap'
or trigger.payload_json.action == 'rotate_left'
or trigger.payload_json.action == 'rotate_right'
%} light.turn_on
{% else %} script.dummy
{% endif %}
data_template:
entity_id: |-
{% if trigger.payload_json.side == 5 %} light.room1
{% elif trigger.payload_json.side == 4 %} light.tradfri_bulb_22
{% elif trigger.payload_json.side == 3 %} light.room3
{% elif trigger.payload_json.side == 2 %} light.room2
{% elif trigger.payload_json.side == 0 %} light.room4
{% endif %}
brightness_step_pct: |-
{% if trigger.payload_json.action == 'rotate_left' %} -10
{% elif trigger.payload_json.action == 'rotate_right' %} 10
{% else %} 50
{% endif %}
- service_template: |-
{% if trigger.payload_json.action == 'slide' %} light.turn_off
{% else %} script.dummy
{% endif %}
data_template:
entity_id: |-
{% if trigger.payload_json.side == 5 %} light.room1
{% elif trigger.payload_json.side == 4 %} light.tradfri_bulb_22
{% elif trigger.payload_json.side == 3 %} light.room3
{% elif trigger.payload_json.side == 2 %} light.room2
{% elif trigger.payload_json.side == 0 %} light.room4
{% endif %}
mode: single
This automation works for me as is, but from a coders point of view is tad ugly so I was wondering “If you’d actually understand how Home Assistant works how’d you go about achieving something similar?”
I think I saw somewhere a way to set ‘variable’ in trigger phase so perhaps I could assign ‘entity_id’ in one centralized location and just use it later.
Also I had to split turn_on and turn_off to 2 different locations as brightness parameters seem to break calling ‘turn_off’ service (I got ““extra keys not allowed @ data[‘brightness_step_pct’]”” if I tried to turn_off entities within same service template.
Saw ‘script.dummy’ solution somewhere in this community and used that (with small mod of using parallel)
alias: dummy
sequence:
- delay:
milliseconds: 1
mode: parallel
max: 50