Replacing MQTT and python Logic with Home Assistant

A newbie to Home-Assistant and will like to migrate my application from the MQTT (mosquitto) and python control logic to HASS and I really don’t know how to begin.

I have arduino(s) that receives all instructions and collects all the necessary data from all sensors and publish such information to the broker.

A python script is subscribed to all the topics and upon getting such topics triggers a sub process that handles the calls. If it’s a topic that requires further action then the new process access a dictionary to see the action that it’s to take and inturn publish the required action and where another arduino that is to perform the action gets it’s information, carries out the action and then publishes back a result, and the circle continues. This is a very simple and extremely effective system.

From what I have gathered so far, I’m to create the MQTT Broker Component on the config.yaml which will have a trigger assigned to that Component and this will have some Conditions (optional) and then Actions follows. Am I correct with this if not please help with clarification?

Is it possible for HASS conditional logic to access a python dictionary? If so kindly point me in the direction to get such an information.

If not can it access a db (postgres)?

Is the home assistant able to trigger sub process so that I can effectively utilise the CPU cores?

If there are similar topics that have answered my questions I will really appreciate if I can be pointed in the right direction.

First of all, you need to understand the HA concept of sensors, which are things that receive data from the outside world. An MQTT sensor (or binary_sensor which is separate) subscribes to a topic, and sets it state according the payload in the message.

Automations are then generally triggered from the state of the sensor.

You can also trigger automations directly from MQTT messages if necessary, but this looses the state of the sensor, which can be useful.

The standard way of writing automations is using the yaml automation scripting, which is a pain if you are used to Python. I would suggest you look at appdaemon which enables you to write automations in Python.

Thanks for the response, I will prefer to take this one step at a time so that I can fully grasp properly.

I want to turn on the light in a particular room and I press the switch,

The Arduino_switch that captures all switch inputs, captures that port_50 has just been pushed it then publishes

mos_pub -h <host_ip> -p -t <switch/light/topic> -m <arduino_address/port_50>

In my present case (port_50) is defined as boys_bedroom and the relay for the lights in located on (port_20 of arduino_lights) in the dictionary and the script then does

mos_pub -h <host_ip> -p -f <arduino_lights/topic> -m <toggle/port_20>

the arduino for lights is already subscribed to arduino_lights and immediately toggles because the action it received in the message is

This is a simple call flow.

So going back to this

image

Outside World = Arduino Switch (send the triggered switch), Arduino Light Control (toggle the light)

What I understand from the quote is that MQTT Sensor is the Arduino Light Control that receives the final toggle instruction and then toggles relay 20, please amd I right with this?

Once this is clear I will be able to ask my next question

FIrst of all, it is again important to clarify terminology. In HA, a switch is something that HA can control by sending commands to it. From your description, this is best described in this forum as a button, which will avoid confusion.

You could describe a button press as a sensor, but since it doesn’t have a state (as far as HA is concerned) it is probably sensible just to use the MQTT message directly as a trigger to an automation.

In HA terminology, your light is an MQTT Light component. HA has commands to turn on the light, which results the the MQTT message being sent out. It also can detect the state of the light changing from a message sent by the light, so that the state of the light can be reflected in HA.

So in your scenario, the button press would generate an MQTT message, which would trigger an automation in HA to turn on (or off, or toggle) the light. Turning on the light in HA would generate an MQTT message to the physical light - and optionally wait for a status response to indicate that the light had actually turned on.

The light could also be turned on by using the web based UI.

Edit:
It did occur to me that if all your buttons publish to the same topic, you could define an MQTT sensor for that topic. Automations could then trigger off the state of the sensor, which will be set to the payload of the most recent mqtt message received. Just an alternative way of looking at the input.

1 Like

I feel like hugging you right now for this explanation I have started to move into the HA terminologies.

So from my understanding we have introduced 2 buttons

Button 1 (on the Wall in the room) which is Outside of the HA, has (NO STATE from HA perspective)
Button 2 (Web UI) {I hope I’m right with this terminology} which is in the HA and has (STATE from HA point)

Can both of these trigger the same automation rule?

No, there is one button (an external input) and one light component.

The light component has a toggle on the ui, which is used to represent the state of the light and alter its state by sending MQTT commands.

You can trigger automations from the state of the light component, but it isn’t necessary just to turn on the light.

The automation triggered by receiving an MQTT message from the button would change the state of the Light Component, which would send an MQTT message to the physical light.

Clear I appreciate your prompt response

From what I’ve gotten from this it looks like I have to create a separate automation trigger for each button component, is there the possibility of a lookup table for mapping of the button port information to the component to be triggered?

Also please what is a payload?

Thanks

Baba @oluwasegun74,

Well I will try to break it down. First let me outline what I think I understand your setup is. You have a bunch of Arduino(s) around your home, which you use to sense button presses, and once pressed publishes to a broker. Your broker is running on a system (Linux or Windows) which has some python scripts that do some stuffs, and send the data back to the Arduino. Like the same Ardunio used to sense the button will activate relay in the same room? Also can you explain what exactly your python scripts do and your need to

Once understood, it will be easier for one to point you in the right direction.

Regards

I believe you would be able to use templating and use only one automation, but I am not an expert in this area as I use appdaemon for all my automations. You only need one class using appdaemon, but that is a whole other topic.

It is the data part of the MQTT message - in contrast to the topic such as switch/light/topic

Bros @Odianosen25,

You are some how correct, you know that is the closest we can get to hue lights in our environment.

My broker is running on Linux as per pi level and there I’m also now wanting to include generator control and some other things.

But first let me understand the basics before I start to move to the next level because in the world of Naija na gsm/sms be our internet so that one na another talk.

This thing is very frustrating and the documentation is not helping at all, it just jumping from one thing to another. I’ve only used template when I was rendering some html but right now this is a different ball. In python script I would have imported the dictionary and then pass the dictionary in as variable to the template right now I don’t know how to import this in HA. However I have done somethings.

This is what I have done to my configuration.yaml file

sensor:

  • platform: mqtt
    state_topic: “#”

mqtt:
broker: localhost
port: 1883

This is what I see when a button press publish

This is showing the hostname_of_arduino/ip_address/button pressed i don’t know what do do again after this.

I know that I should put this in automation.yaml and this would be my trigger.payload but then I still don’t know what to do after that.

Normally what I would have done in my script is extract the button press originating hostname and the port (which is what is in my trigger.payload) use this values to lookup in the dictionary to get the switching arduino hostname and the switching port to be toggled and publish this message as the switching message, please how can i achieve this?

Is it possible for me to call a python script from the Trigger, Condition, or Action?

If not how can you direct me?

Thanks

This looks like good progress. The sensor seems to be working fine.

I think next you need to define your MQTT Light component, so that you have something to turn on. It needs to have a command_topic set to what you use to turn the light on.

You should then be able turn the light on and off from the gui.

The action of your automation would then be light.turn_on, which will send the mqtt command to the light.

I think you may be able to do what you would like to do with tables using the Python Scripts component, but again I have never used it as I use appdaemon, which has a different approach.

But you should probably do a simple yaml automation first, if only to convince yourself that you never want to do another one. :wink:

What I read about the appdeamon is that it’s supposed to work in conjunction with this, I getting already frustrated it’s taking over 96hrs of effort and for me that’s too much time. However in the last 24 to 48 hrs you have giving some courage to move on thanks for this will try to make sense of what you just said and get back with any issues

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.

You still seem to be trying to use HA as a python programming environment. Let me translate this into HA concepts for you.

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

This a sensor, and let us call it button_3. This will be defined in ha as

sensor:
  - platform: mqtt
    name: button_3
    state_topic: Arduino-east/button

Whenenver HA receives a message with that topic, its state will be set to the payload of the message, and any automations triggered by the state change will be run. The automation (either with appdaemon, yaml or a python script) is where you will need to decode the payload (from the state) and decide which light to control. It will do this by calling the HA service (light.turn_on, light.turn_off, light.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 data[3] (Arduino-Switch-Center) -m data[2]

This in HA terms is an mqtt light. It needs to be defined as

light:
  - platform: mqtt
    name: light_3
    command_topic: home/security/lights/state:::20
    state_topic: home/security_lights_state
    payload_on: ON
    paylaod_off: OFF

I’m afraid HA cannot handle having the command in the topic, so you will need to change that if you want to use HA. It is a very unusual way of using MQTT - the topic normally defines a target and the payload defines the operation.

Using the toggle on the gui or the HA light.turn_on service will send an MQTT message with the command_topic and payload ON, which will turn the light on.

Obviously, there is a one-one relationshitp between physical lights, and mqtt lights, so you will need to define an mqtt light component for each physical light you have.

On the sensor side, you have a separate sensor for each topic you receive, so its simpler to define a separate topic for each button, although you can decode more within the automation if necessary.

Hello,

Now we are making a headway,

Arduino-east is a trigger point it does not know the state of the light it’s so bad it does not know the light that the pressed button is controlling, because the intelligence lies with HA, so in the payload of the message from arduino-east there is no state so what you’ll be having as payload is the address of the button that was pressed which is Arduino-east/<its_ip_address>/3

How will HA now treat the information from the arduino-east every time as a state change to always trigger the Automation process meeting the condition below?

This is the begin question then we will move to the next after this.

I’m not sure this is the question you are asking, but HA will simply run all automations that are triggered when the state is changed. It is up to the code of the automation what to do with the information.

How can I relate automation process to state topic?

Thanks

There’s literally an example of an MQTT automation

Thanks so much for your support the switching part works and this is what I have for my configuration

light:

  • platform: mqtt
    name: “Living Room Lights”
    command_topic: “Arduino-Switch-Center:::home/security_lights_state:::20”
    state_topic: “home/security_lights_state”
    payload_on: “ON”
    payload_off: “OFF”

Right now from the UI it is possible to switch on a light, so the basics of what is required to switch ON and OFF Items are now clear and what is expected on my Arduino sketch to control switching is now understood I will now move to the external trigger.

Please where can I get a good explanation of appdaemon?

Thanks