Hello,
I set-up MQTT communication of a Shelly Pro 4PM between HA and Domoticz with success. The following code do the job for one of the four switches
Now come the time either to duplicate the code 3 times in order to support the 4 switches and change some “variables”. The variables are entity id’s and idx values that depends on entity id’s. Is there a better way to do that, in order to avoid expansion of the code which turns into more maintenance? Any ideas?
Thank you
Instead of calling a different service turn_off / turn_on based on a condition, set a variable that will be set to “on” or “off” depending on “trigger.payload_json.nvalue” and then call the service: switch.turn_{{ some_variable }}
I reduced it to two automations, one publishes to Domoticz and the other receives from it. It’s possible to combine them but not completely necessary.
The following two examples are not tested so they may require a few adjustments.
For receive from DZ, I removed the sensors since DZ do not publish anything (you could not guess), and I inverted the on/off conditions apart from that this is perfect
Thank you very much again for your very valuable input, I made progress in mastering HA which is quite a challenge I did not know how to use variables, despite my search in the doc, I could not find it. I discovered the get function as well.
trigger.to_state.state contains the current state value of the entity that triggered the State Trigger.
For example, if switch.pressostat changes from on to off it will trigger the State Trigger and the value of trigger.to_state.state will be off.
Your version takes an extra step. It queries the State Machine by using the states() function to get the current state value of switch.pressostat (using trigger.entity_id). That value is already available in the trigger object via trigger.to_state.state.
Sure, you are right I must have had something else preventing it to work correctly at the beginning of the debugging work. So the final code indeed includes:
trigger.to_state.state
instead of
states(trigger.entity_id)
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.
I used this technique for many different entities but there is a downside. I noticed all automations listening to domoticz/out/# are triggered by any domoticz devices (including those that are not needed) which generates a lot of trafic. Since I decided to keep Domoticz and HA, I changed the Domoticz MQTT interface to Hierarchical. This allow to select Domoticz devices that are allowed to talk to MQTT reducing drastically trafic.