It works!
The problem with re-connecting (that I didn’t know I had) was solved by removing the client_id
from mqtt.yaml (it didn’t work to give different client_id on RPi and NUC. I had to remove the client_id
line altogether). After doing that, the sensor data showed up in the homeassistant.log (but wasn’t read by homeassistant mqtt sensor).
Adding \"
around the template value (for state
only, maybe because this one is not a number?) makes home assistant (on NUC) recognize the whole json string. So the automation.yaml on RPi needed to be:
- alias: plant sensor 1 send state
trigger:
platform: time
seconds: 30 # triggers every 30 seconds
action:
- service: mqtt.publish
data:
topic: plant_sensor/1
# payload_template: "{ \"state\": {{ states('plant.basil') }}, \"battery\": {{ states('sensor.plant_sensor_battery') }}, \"temperature\": {{ states('sensor.plant_sensor_temperature') }}, \"conductivity\": {{ states('sensor.plant_sensor_conductivity') }}, \"light_intensity\": {{ states('sensor.plant_sensor_light_intensity') }}, \"moisture\": {{ states('sensor.plant_sensor_moisture') }} }"
payload_template: "{ \"state\": \"{{ states('plant.basil') }}\", \"battery\": {{ states('sensor.plant_sensor_battery') }}, \"temperature\": {{ states('sensor.plant_sensor_temperature') }}, \"conductivity\": {{ states('sensor.plant_sensor_conductivity') }}, \"light_intensity\": {{ states('sensor.plant_sensor_light_intensity') }}, \"moisture\": {{ states('sensor.plant_sensor_moisture') }} }"
Compare to the old line (commented out). The message received is now:
plant_sensor/1 { "state": "problem", "battery": 99, "temperature": 32.4, "conductivity": 765, "light_intensity": 112630, "moisture": 45 }
The mqtt.yaml is very minimal; on NUC:
broker: 127.0.0.1
port: 1883
protocol: 3.1
and on the RPi:
broker: 192.168.0.100
port: 1883
protocol: 3.1
So thanks @ @gpbenton, I think I’ve solved the problems, including the one I didn’t even know I had
And btw, I had some old logs in the mosquitto.log (from before removing client_id). I don’t think these are very helpful, or important anymore?:
1527844867: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527844867: Config loaded from /mosquitto/config/mosquitto.conf.
1527844867: Opening ipv4 listen socket on port 1883.
1527844867: Opening ipv6 listen socket on port 1883.
1527844876: New connection from 127.0.0.1 on port 1883.
1527845021: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527845021: Config loaded from /mosquitto/config/mosquitto.conf.
1527845021: Opening ipv4 listen socket on port 1883.
1527845021: Opening ipv6 listen socket on port 1883.
1527845029: New connection from 127.0.0.1 on port 1883.
1527845029: New client connected from 127.0.0.1 as homeassistant_mqtt (c1, k60, u'homeassistant').
1527846821: Saving in-memory database to /mosquitto/data/mosquitto.db.
1527848622: Saving in-memory database to /mosquitto/data/mosquitto.db.
1527850423: Saving in-memory database to /mosquitto/data/mosquitto.db.
1527851733: Client homeassistant_mqtt disconnected.
1527851733: Error in poll: Interrupted system call.
1527851733: mosquitto version 1.4.12 terminating
1527854236: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527854236: Config loaded from /mosquitto/config/mosquitto.conf.
1527854236: Opening ipv4 listen socket on port 1883.
1527854236: Opening ipv6 listen socket on port 1883.
1527854243: New connection from 127.0.0.1 on port 1883.
1527854243: New client connected from 127.0.0.1 as homeassistant_mqtt (c1, k60, u'homeassistant').
1527854852: Error in poll: Interrupted system call.
1527854852: mosquitto version 1.4.12 terminating
1527854945: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527854945: Config loaded from /mosquitto/config/mosquitto.conf.
1527854945: Opening ipv4 listen socket on port 1883.
1527854945: Opening ipv6 listen socket on port 1883.
1527854952: New connection from 127.0.0.1 on port 1883.
1527854990: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527854990: Config loaded from /mosquitto/config/mosquitto.conf.
1527854990: Opening ipv4 listen socket on port 1883.
1527854990: Opening ipv6 listen socket on port 1883.
1527855000: New connection from 127.0.0.1 on port 1883.
1527855000: New client connected from 127.0.0.1 as homeassistant_mqtt (c1, k60, u'homeassistant').
1527855683: Client homeassistant_mqtt disconnected.
1527855683: Error in poll: Interrupted system call.
1527855683: mosquitto version 1.4.12 terminating
1527855901: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1527855901: Config loaded from /mosquitto/config/mosquitto.conf.
1527855901: Opening ipv4 listen socket on port 1883.
1527855901: Opening ipv6 listen socket on port 1883.
1527855907: New connection from 127.0.0.1 on port 1883.
etc., but I get no new entries upon reboot. As in zero new lines; I deleted all the content of the file, now it’s empty even after multiple reboots.