"Once again we will use the power of Home Assistant to add switches to the interface and to allow these switches to publish messages with MQTT.
This is a simple process if you follow these instructions:
add an editor to home assistant
go to the add on store and select "File Editor"
install it run it
then you can click on "open Web user interface" this will magically open the file editor and the famous "YAML file" of home assistant
This file handles all teh configuration of your HA. With a new project it is almost empty.
We will add a MQTT switch in this file.
Any time we write something into this file the consistency is cheked and the gren mark on top right corner should remain green. If becoming red it is most likely an error of indentation into your YAML file.... Beware of blank space... they go usually two by two !
To add a switch copy paste these lines:
mqtt:
switch:
- unique_id: S1C1
name: "Serre1 Coil1"
state_topic: "garden/S1/C1"
command_topic: "garden/cmd/1/1"
payload_on: "[{\"id\":1,\"type\":1,\"data\":1}]"
payload_off: "[{\"id\":1,\"type\":1,\"data\":0}]"
optimistic: true
qos: 0
retain: true
It tells the following:
- we use an MQTT integration
- we add a serie of switches (only one right now)
- this switch has "serre1 Coil1" name (you can change it !)
- more important: this switch will publish an MQTT message to the topic : "garden/cmd/1/1"
- and it will pusblish these JSON messages respectively when On or Off
payload to topic garden/cmd/1/1 à 14:48 :
[
{
"id": 1,
"type": 1,
"data": 1
}
]
- the switch as an "optimistic" behavior. Even when its state is unknown (first boot) it will be displayed at the off state
- the Quality Of Service (QoS) is set at its lowest level : 0 (see more here) you can change it up to level 2
- Extremely important : this message wil be retained on the broker. This means that any time a client will "subscribe" to this topic, the broker will push the latest received message to the client. So, if the current configuration is kept on the broker side, then any time our Rezodo GTW0 will subscribe to these messages, it will be updated with the latest command sent to the broker : cool !
Now we can save the YAML file with these lines inserted.
We then need to restart the Home assistant (go to the developer tools, on top of the page)
And that's it you have a nice switch on your HA main page
You can test it using the MQTT add on and see that it works when you change this switch while listening to the " garden/cmd/1:1" topic
As it works for one switch we will now add the 7 missing to configure our Rezodo.
Simply copy paste these lines into the YAML
mqtt:
switch:
- unique_id: S1C1
name: "Serre1 Coil1"
state_topic: "garden/S1/C1"
command_topic: "garden/cmd/1/1"
payload_on: "[{\"id\":1,\"type\":1,\"data\":1}]"
payload_off: "[{\"id\":1,\"type\":1,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S1C2
name: "Serre1 Coil2"
state_topic: "garden/S1/C2"
command_topic: "garden/cmd/1/2"
payload_on: "[{\"id\":1,\"type\":2,\"data\":1}]"
payload_off: "[{\"id\":1,\"type\":2,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S1C3
name: "Serre1 Coil3"
state_topic: "garden/S1/C3"
command_topic: "garden/cmd/1/3"
payload_on: "[{\"id\":1,\"type\":3,\"data\":1}]"
payload_off: "[{\"id\":1,\"type\":3,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S1C4
name: "Serre1 Coil4"
state_topic: "garden/S1/C4"
command_topic: "garden/cmd/1/4"
payload_on: "[{\"id\":1,\"type\":4,\"data\":1}]"
payload_off: "[{\"id\":1,\"type\":4,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S2C1
name: "Serre2 Coil1"
state_topic: "garden/S2/C1"
command_topic: "garden/cmd/2/1"
payload_on: "[{\"id\":2,\"type\":1,\"data\":1}]"
payload_off: "[{\"id\":2,\"type\":1,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S2C2
name: "Serre2 Coil2"
state_topic: "garden/S2/C2"
command_topic: "garden/cmd/2/2"
payload_on: "[{\"id\":2,\"type\":2,\"data\":1}]"
payload_off: "[{\"id\":2,\"type\":2,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S2C3
name: "Serre2 Coil3"
state_topic: "garden/S2/C3"
command_topic: "garden/cmd/2/3"
payload_on: "[{\"id\":2,\"type\":3,\"data\":1}]"
payload_off: "[{\"id\":2,\"type\":3,\"data\":0}]"
optimistic: true
qos: 0
retain: true
- unique_id: S2C4
name: "Serre2 Coil4"
state_topic: "garden/S2/C4"
command_topic: "garden/cmd/2/4"
payload_on: "[{\"id\":2,\"type\":4,\"data\":1}]"
payload_off: "[{\"id\":2,\"type\":4,\"data\":0}]"
optimistic: true
qos: 0
retain: true
Save it, restart the HA and go to the main page
Here it is! 8 switches which can command your Rezodo.
Well ... almost... We still need to configure our Arduino code to register to the messages we want.
Open the MQTTsubscription.h file into your FDRS libray folder and add these lines:
client.subscribe("garden/cmd/1/1");
client.subscribe("garden/cmd/1/2");
client.subscribe("garden/cmd/1/3");
client.subscribe("garden/cmd/1/4");
client.subscribe("garden/cmd/2/1");
client.subscribe("garden/cmd/2/2");
client.subscribe("garden/cmd/2/3");
client.subscribe("garden/cmd/2/4");
Save the file and recompile... Now the Rezodo will receive the command messages any time it wakes up.
Cool again !
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.