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.
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)
Have ppl been experiencing problems with the dice?
I was tossing mine up and down a bit (to get ‘falling’ or like action) and managed to “freeze” the cube. It nolonger sent anything into zigbee2mqtt (and as such nothing come to HA)
after removing the battery and putting it back it started to work again.
Just a suggestion, when using the community forum for research, try to avoid employing examples that are more than a year old (maybe even just six months). Whatever ancient resource you used has led to composing an automation containing syntax that was deprecated well over a year ago and archaic techniques (like script.dummy) that are no longer needed.
Here’s an example using currently available scripting techniques.
You’re welcome but your 'small modifications ’ introduced unnecessary elements as well as deprecated conventions. If appears it’s much more challenging to forego the practices you learned from archaic examples.
A YAML dictionary’s keys can be any of several types including integers. If you used the Automation Editor to compose the automation, one of its annoying quirks is to wrap integer keys to make them strings. It’s not due to a YAML limitation.
Be aware that Home Assistant employs native typing which means it infers a template value’s type based on its appearance. For example, if it looks like a number, the type of the template’s result will be a number (int or float). In this case, native typing wasn’t involved (because the string filter was applied to the first argument of the get method) but it’s something you should keep in mind.
Do you want me to revise your second example or are you already creating an enhanced version of it?
I was surprised at the int->str change as I’ve used yaml dicts with integers in python before.
I did not realize it was the editor that changed the dict keys into str should’ve looked at automations.yaml more carefully.
Casting of the value would’ve been needless if I would’ve realized to edit .yaml file instead of using the editor.
If you are willing to give me more examples of best practices I am keen to learn and possibly it could serve as convention compass for someone else as well.
As far functionality goes I think cube now does more or less all the things I could come up with it.
(whenever it actually senses stuff and sends it and does not lock up)
I noticed that there were some samples which used the cube in a way it had ‘states’ one for light control and other for AV control and mode could’ve been switched by shaking or dropping the cube.
Stateful cube would add more use possibilities.
Would such functionality still be implemented with a ‘global’ cube state which would be updated by different automations and then that state would be used in the Contition clause in the beginning of the automation?
If that is recommended path I’ll give it a go in next stage of tweakings
Here’s a revised version of the automation. The lights variable is now simply a list instead of dict (thereby avoiding the complication caused by the Automation Editor’s quirk).
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.