Setting up MQTT sensors for the first time, and hitting issues when trying to read Json value.
On photon/output my device is publishing:
{ "counter": 722, "switch": "OFF" }
Here is a sample Json string, copied out of “dev-state”, which matches my expectations:
{ “counter”: 722, “switch”: “OFF” }
Here are my test sensor/switch definitions:
sensor:
- platform: mqtt
state_topic: 'photon/output'
name: 'photon_count'
unit_of_measurement: 'uptime'
value_template: '{{ value_json.counter }}'
switch:
- platform: mqtt
name: "photon"
command_topic: "photon/input"
state_topic: "photon/output"
value_template: '{{ value_json.switch }}'
retain: true
These are based on the docs at:
https://home-assistant.io/components/sensor.mqtt/
https://home-assistant.io/components/switch.mqtt/
The problem is that the sensor displays it’s value as the full Json string, not “722”. The value_template is being ignored. Previously, I published the sensor value as a simple “722”, and everything worked as expected.
There are no relevant errors in the log.
The switch can send commands correctly, but doesn’t read the current switch state. All of the MQTT values appear to be published as I would expect when I snoop via my phone.
Testing on the dev-template page:
{% set value_json= { “counter”: 722, “switch”: “OFF” } %}
{{value_json.counter}}
Displays “722”, as expected.
I feel like it’s something silly, but I don’t see what.