Passing values from a networked RPi (without HASS.IO) to a HASS.IO RPi via MQTT

I need some guidance figuring out the basic logic for the following project:

I want to pass along system and other data from a networked RPi (that has no HASS.IO installation) to the HASS.IO RPi. In the simplest case, that would be the CPU temperature readings of the networked RPi.

I have learned how to publish MQTT messages from the HASS.IO to the other RPi and that works very well.

But how do I send MQTT messages with values back from the other RPi to HASS.IO so that they are being displayed in a HA panel?

Or is this a time for appdaemon? Any thoughts?

Here’s an example with a bash script.

1 Like

Sounds like the exact match to what I am looking to do. Thanks Rainer, will try this afternoon and report back!

I have Paho installed as MQTT client and I just can’t figure out what

/usr/bin/mosquitto_pub -r -t "$TOPIC" -m "$PAYLOAD" -h "$MQTT"

would be for Paho in a non Python shell script.

Paho is a python client.
For the bash you need mosquitto-clients.
sudo apt install mosquitto-clients

1 Like

Sorry for the delay, I now have MQTT with python scripts up and running. Now I just have one question left:

create mqtt sensor in ha

Would you have an example config entry for the sensor acting upon mqtt payloads?

You can take the MQTT Sensor or the MQTT Binary Sensor for it.
Here’s the mqtt sensor for the example script.

sensor:
  - platform: mqtt
    name: ha_gw01_i2cbus
    state_topic: 'home/sensors/minibian/i2c_bus'
1 Like

So I got the MQTT Binary Sensor working fine. Now it’s time to get the MQTT switch working.

I am a bit confused to what exactly state/command/availability topics do. Availability topic as I understand it is a general message if the switch can be reached. It is controlled with “online” and “offline”.
State topic would then be a regular feedback message from the controlled device (as @VDRainer described it in the bash script). I can control it by sending messages with “ON” and “OFF”.
What doesn’t work in my case is the Command message. When I send “ON” or “OFF” to the command_topic nothing happens.

This is what my configurations.yaml looks like:

# MQTT Switch
switch:
  - platform: mqtt
    name: "Bilderrahmen an/aus"
    icon: mdi:panorama
    state_topic: "frame/monitor"
    command_topic: "frame/monitor/set"
    availability_topic: "frame/monitor/available"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

I am looking for clues why messages to the command_topic don’t cause the Switch to to turn on/off…

And if it’s of any help finding out what I may be doing wring, this is the MQTT config part with the topics.

  "mqttusers": [
    {
      "username": "xx",
      "password": "xxx",
      "readonly": false,
      "topics": [
        "frame/monitor",
        "frame/monitor/set",
        "frame/monitor/available"
      ]
    }
  ]

I found out where my logic was wrong: command_topic can only be activated through flicking the HA switch directly, it apparently can’t be remote controlled through MQTT.

Now everything works fine! :hugs:

For those who are interested in the python script I am using to pass on the information on the display status:

#!/usr/bin/env python3

import paho.mqtt.client as mqtt
import subprocess

client = mqtt.Client()
client.username_pw_set( "username" , "password" )

client.connect("brokerIP", port, 60)

# Send availability notice
client.publish("frame/monitor/available", "online")

# hdmi status display_power
display_status = subprocess.run(['vcgencmd', 'display_power'], stdout=subprocess.PIPE).stdout.decode('utf-8') # check display status
display_status_value = display_status[14:15] # cut display status from result
print(display_status_value) # 0 oder 1

if display_status_value == "0":
    client.publish("frame/monitor", "OFF")
else:
    client.publish("frame/monitor", "ON")

And for the CPU temperature:

#!/usr/bin/env python3

import paho.mqtt.client as mqtt
import subprocess

client = mqtt.Client()
client.username_pw_set( "username" , "password" )

client.connect("brokerIP", port, 60)

# Give availability notice
client.publish("frame/cpu_temp/available", "online")

# CPU temperature
cpu_temp = subprocess.run(['vcgencmd', 'measure_temp'], stdout=subprocess.PIPE).stdout.decode('utf-8') # measure CPU temperature
cpu_temp_value = cpu_temp[5:9] # cut temperature from result
cpu_temp_value_int = int(round(float(cpu_temp_value), 0)) # convert string into number, round and make integer
print(cpu_temp_value_int)

client.publish("frame/cpu_temp", cpu_temp_value_int)