sapnho
(Wolfgang)
October 28, 2018, 9:06am
1
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.
I’m using mqtt sensors for this.
Install mosquitto clients on your remote pi
run the command or a script with crontab every 2 minutes
publish the values to mqtt topic
create mqtt sensor in ha
Example script:
#!/bin/bash
MQTT='192.168.123.123'
TOPIC='home/sensors/minibian/i2c_bus'
PAYLOAD=$(/usr/sbin/i2cdetect -y 1 | egrep '^20' | cut -d' ' -f2-9)
/usr/bin/mosquitto_pub -r -t "$TOPIC" -m "$PAYLOAD" -h "$MQTT"
1 Like
sapnho
(Wolfgang)
October 28, 2018, 9:44am
3
Sounds like the exact match to what I am looking to do. Thanks Rainer, will try this afternoon and report back!
sapnho
(Wolfgang)
October 30, 2018, 8:28pm
4
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.
VDRainer
(🍻)
October 30, 2018, 10:25pm
5
Paho is a python client.
For the bash you need mosquitto-clients.
sudo apt install mosquitto-clients
1 Like
sapnho
(Wolfgang)
November 11, 2018, 7:23am
6
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?
VDRainer
(🍻)
November 11, 2018, 7:42am
7
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
sapnho
(Wolfgang)
November 11, 2018, 11:33am
8
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…
sapnho
(Wolfgang)
November 11, 2018, 11:35am
9
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"
]
}
]
sapnho
(Wolfgang)
November 11, 2018, 12:23pm
10
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!
sapnho
(Wolfgang)
November 11, 2018, 7:38pm
11
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")
sapnho
(Wolfgang)
November 11, 2018, 7:39pm
12
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)