I wonder if anyone could suggest the approach for this idea. I have 27 Sonoffs basic with GPIO14, TX and RX soldered with wires connected to the physical light switches. Everything works perfectly on Tasmota but now I need to set functions to TX and RX on Home Assistant, total of 54 commands (TOGGLE), no need for POWER1 TOGGLE since it is already configured on each light entity created. Also I want to use the HOLD message, so it will result on 135 commands (including POWER1 topic).
What do you guys think it would be the best approach? Create 135 automations looking the MQTT message and executing a script? Create 135 MQTT switches?
Something like this:
Total of 27 relays:
81 physical switches
54 TOGGLE commands
81 HOLD commands
It would be very nice to have it on a single automation so the action could identify the trigger and execute a script…
Hello! Thanks for your time, I really appreciate it.
Sorry, I was definitely not clear enough explaining my ideia.
That automation sample is the one I use for Xiaomi wireless buttons, over Zigbee2MQTT protocol, so that is what the click type is. I like the way the service template is created using the json information from the message.
What I really need is to use my Sonoffs Basics with 3 physical buttons, one for the real relay and 2 for virtual relays that results on POWER2 and POWER3 messages over MQTT. Both topics can broadcast TOGGLE and HOLD commands, so excluding the POWER1 TOOGLE command (already set on the light entity), I can use POWER1HOLD, POWER2TOOGLE, POWER2HOLD, POWER3TOGGLE and POWER3HOLD to trigger scripts using automation (MQTT trigger).
I am not sure the best way to simplify this process of calling the correct script by stripping the full topic sent by the device, for example:
If I press TOGGLE on POWER2 using the device s_1_luz_tv I get stat/s_1_luz_tv/RESULT = TOGGLE. So I need to figure out how to strip this message to know which device and what command is sent and use it on the service_template to simplify the script entity_id, so this example would be script.interruptor_s_1_luz_tv_POWER2_TOGGLE.
So just one automation could call any of the 135 pre made scripts.
This process will be used with other POWER# topics and HOLD/TOGGLE messages to call different scripts.
All scripts would be pre-configured and named accordingly, so I can change it later if needed.
Yes! That is what I did. You need to set the module to generic and then set two pins as relays and switches, so it thinks it is activating relays and sends POWER2 and POWER3 messages.
I use RX and TX just like GPIO14, you can do everything with them. I have both momentary and toggle switches, you just need to change switchmode in console. Also, you can only use HOLD with toggle switches, obviously. Good luck!
As simple as that or as complex as I need. The main problem here is how to call the correct script given the topic and message information only by the automation.
I’m wondering if you even need to make a script for each one. Anyways, this should work assuming the stat/+ bullshit works and if this is how your topics come across:
- alias: mqtt
trigger:
- platform: mqtt
topic: stat/+
condition:
- condition: template
value_template: "{{ trigger.payload_json.items() | list | length >= 1 and trigger.topic.split('/') | length == 3 }}"
action:
- service_template: >
{% set args = trigger.payload_json.items() | list %}
{% set power, action = args[0] %}
{% set a, scriptname, c = trigger.topic.split('/') %}
script.interruptor_{{ scriptname }}_{{ power }}_{{ action }}
Just to make sure: the scriptname variable would be the MQTT topic for the device, correct? The script name will adapt to relative topic and actions…
Also: I am thinking about the trigger condition… Wouldn’t be better to have de devices topics (list above) as conditions? Since I use Zigbee2MQTT and many other MQTT devices, it could only trigger by the topics I want.
Hello my friend! After a couple of days of testing this worked out for me. Since Power2 and Power3 sends TOGGLE commands, I’ve created individual automations (so I can disable them by their function) to call scripts named after the trigger action/topic result:
Topics/Subscriptions
Messages in MQTT are published on topics. There is no need to configure a topic, publishing on it is enough. Topics are treated as a hierarchy, using a slash (/) as a separator. This allows sensible arrangement of common themes to be created, much in the same way as a filesystem. For example, multiple computers may all publish their hard drive temperature information on the following topic, with their own computer and hard drive name being replaced as appropriate:
sensors/COMPUTER_NAME/temperature/HARDDRIVE_NAME
Clients can receive messages by creating subscriptions. A subscription may be to an explicit topic, in which case only messages to that topic will be received, or it may include wildcards. Two wildcards are available, + or #.
+ can be used as a wildcard for a single level of hierarchy. It could be used with the topic above to get information on all computers and hard drives as follows:
sensors/+/temperature/+
As another example, for a topic of "a/b/c/d", the following example subscriptions will match:
a/b/c/d
+/b/c/d
a/+/c/d
a/+/+/d
+/+/+/+
The following subscriptions will not match:
a/b/c
b/+/c/d
+/+/+
# can be used as a wildcard for all remaining levels of hierarchy. This means that it must be the final character in a subscription. With a topic of "a/b/c/d", the following example subscriptions will match:
a/b/c/d
#
a/#
a/b/#
a/b/c/#
+/b/c/#
Zero length topic levels are valid, which can lead to some slightly non-obvious behaviour. For example, a topic of "a//topic" would correctly match against a subscription of "a/+/topic". Likewise, zero length topic levels can exist at both the beginning and the end of a topic string, so "/a/topic" would match against a subscription of "+/a/topic", "#" or "/#", and a topic "a/topic/" would match against a subscription of "a/topic/+" or "a/topic/#".
I was wondering if you could help me again, with a new take on this automation.
I’ve learned to work with rules inside Tasmota so now I can control all switches by using buttons, so now I just publish mqtt messages when a any of the three buttons are pressed (toggle and hold functions).
Trigger seems to be working fine but I have not been able to parse just a part of the trigger.json_payload since I call a script name by using just the BUTTON# part and final “action” part to call the correct script.
Error while executing automation automation.botoes_sonoff. Error rendering template for call_service at pos 1: UndefinedError: 'dict object' has no attribute 'split'