I have the table below,
title = {button_arduino_hostname, button_port, topic, switch-arduino_hostname, switch_port}
data = {
[Arduino-east, 3, home/living-room/switch/state, Arduino-Switch-Center, 20],
[Arduino-west, 3, home/kitchen/switch/state, Arduino-Switch-lower, 20],
.,
…
[Arduino-347gd, 12, home/security/lights/state, Arduino-Switch-lower, 46],
}
Imagine I press the button 3 on Arduino east , it is going to publish
mos_pub -h <mqtt_ip> -p <mqtt_port> -t Arduino-east/button -m Arduino-east/<its_ip_address>/3
HA is subscribe to all messages so it gets this message.
How do i get HA to call a script or appdaemon?
it’s function is to import the table and split the message with “/” as separator such that I have
variable[0] = Arduino-east
variable[1] = ip_address_of_arduino
variable[2] = 3 (which is the button pressed)
then script looks through the table for where data[0] = variable[0] and data[1] = variable[2],
Then script will publish a new mqtt_command with this details
mos_pub -h <mqtt_ip> -p <mqtt_port> -t data[3] (Arduino-Switch-Center) -m data[2] (home/security/lights/state) :::data[4] (20) ::: toggle
if you analyse the message been sent (home/security/lights/state:::20:::toggle) it is the light_switch state_topic the port where the light is connected to on the arduino and the command toggle.
so splitting the message with “(:::)” it will have
msg[0] = home/security/lights/state
msg[1] = 20
msg[2] = toggle
So the Ardino_swtich_center that is waiting for this command immediately toggles port 20 and will now publish its result
mos_pub -h <mqtt_ip> -p <mqtt_port> -t msg[0] (home/security/lights/state) -m ON or OFF (1/0) which is the present state
Now for UI control, in it’s case it will send state_topic as the variable which is (home/security/lights) to the script and in that case the script or if it’s appdaemon will
check for variable IN data[2] to get the corresponding data[2], data[3] and data[4] then it will now repeat the case of publishing once again
mos_pub -h <mqtt_ip> -p <mqtt_port> -t data[3] (Arduino-Switch-Center) -m data[2] (home/security/lights/state) :::data[4] (20) ::: toggle
And the process continues I mean this is so straight forward I don’t know if it’s clear the documentation is not clear at all.