OK I think JSON is handled a bit differently in templating.
entering {{ states.sensor.mqttjsontest }} yields: <template state sensor.mqttjsontest={'Time': '2018-02-11T07:41:02', 'Uptime': 15, 'Vcc': 3.463, 'POWER': 'ON', 'Wifi': {'AP': 1, 'SSId': 'Geogaddi', 'RSSI': 84, 'APMac': '00:11:32:45:90:1F'}}; friendly_name=mqttjsontest @ 2018-02-11T19:41:02.459049+13:00>
I am trying to find the way to properly treat the sensor output as JSON.
I found that within a template string you can do this: {% set value_json=states(‘sensor.mqttjsontest’) %}. After that, value_json looks like a proper json object. But I cannot figure out how to use that object.
in sensors.yaml (it’s not a binary sensor really )
My xiaomi Cube’s data for a rotate event is sent via statestream from my pi to my pc
this is the statestream mqtt topic #homeassistant/binary_sensor/cube_158d000117d706/event
This is the data that comes with it
#{“event_type”:“rotate”, “value”:"-23.5"}
So this is how I extract said data
I am sure they could, using a template in the automation trigger. In Keith’s example, the template to use would be like {{ states.sensor.hassio_cube_158d000117d706_event.attributes.value | float }}
automation:
trigger:
platform: numeric_state
entity_id: sensor.temperature
# Optional
value_template: '{{ state.attributes.battery }}'
# At least one of the following required
above: 17
below: 25
Why not just create multiple MQTT template sensors? I’m doing something like this (from a package definition). Sorry for the YAML anchor stuff which might make this a little obfuscated, but I think you’ll get the idea.
Simply, I define a bunch of different sensors. Each happens to subscribe to the same topic, and each selects a different part of the JSON payload. One advantage of this is that you don’t need to worry about attribute changes kicking off a state-change or not. Since each sensor is unique with only a single value, you can reliably use it in an automation based on the state being updated.
Well - initially I couldn’t get much sense from the sensor at all - even now, I suspect the format is bung (the JSON is hidden within a string). I gave up on it for now to be honest.
But also; if you could define a single sensor for each object, instead of 5 or 6 - then I think things would be a lot tidier. It’s fine if you only have several such devices, but I intend to have up to 15 of these things. In that scenario, 75 sensors might be excessive
But also; if you could define a single sensor for each object, instead of 5 or 6 - then I think things would be a lot tidier. It’s fine if you only have several such devices, but I intend to have up to 15 of these things. In that scenario, 75 sensors might be excessive
Understood, though attributes are not really first-class objects and if the values are important, you’ll likely find yourself fighting with HA. I’m not 100% sure, but I don’t think that you can fire automations based on changes in the state of attributes vs. the state of the entity. And if you want to look at the history, or feed the data into some other database (like in my case, InfluxDB).
All in all, I think that adding more entities isn’t really a huge heavyweight thing if you’re going to use them somehow and need to extract their values.
The example sensor below shows a configuration example which uses JSON in the state topic to add extra attributes. It also makes use of the availability topic. Attributes can then be extracted in Templates;
Example to extract data from the sensor below '{{ states.sensor.bs_client_name.attributes.ClientName }}'
Within HA I can define a sensor like so
sensor rfbridgeResult:
- platform: mqtt
name: “RFBridgeResult”
state_topic: “tele/sonoffRFridge/RESULT”
value_template: ‘{{ value_json.RfReceived.RfKey }}’
expire_after: 20
json_attributes:
- RfReceived
The value template works fine but where im struggling is to sort out the other attributes
if I use the above json_attributes section then the attributes is a single object with the JSON string.
I thought i could use
json_attributes:
-RfReceived.Sync
This results in a sensor called ‘sensor.bedroom_light’. The attributes defined under json_attributes are gleaned using templates like (case sensitive):
{{ states.sensor.bedroom_light.attributes.Vcc }}
Without the json_attributes entry, the values all seem to be lumped in as the ‘state’ and I cannot access them programatically within Home Assistant. So re-reading your post - I think the value_template: ‘{{ value_json }}’ part should free things up so that everything tasmota sends is accessible.