Close

adding switches to home assistant

A project log for Rezodo: Long Range irrigation and weather station

Rezodo aims at building a distributed irrigation system and a weather station with farmers. It's also a fully open platform for IoT learning

jp-gleyzesJP Gleyzes 04/19/2023 at 12:560 Comments

"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:

payload to topic garden/cmd/1/1 à 14:48 :
[
    {
        "id": 1,
        "type": 1,
        "data": 1
    }
]

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