I am having some trouble using templates with json. I am trying to pick a value from a json list within an automation but it doesnt work. I have pasted an example below to highlight the problem. The value of backup_timer_1 is identical to the text I have manually set for variable tim1. Does anyone have any ideas where I am going wrong or is this a limitation of the templating system?
{% set tim1 = { "profile": "Medium", "time": "06:30", "active": "on", "weekday": "on", "weekend": "on" } %}
{% set tim = states.sensor.backup_timer_1.state %}
Doesn't work {{ tim.time }}
Doesn't work {{ states.sensor.backup_timer_1.state.time }}
Does work {{ tim1.time }}
Thanks for your suggestions. I tried these but no joys. It appears you cannot process JSON in an automation template, only in the value_template of a sensor or switch. I will make a feature request.
The template is seeing your sensor state as a string. The value_templates where you see this working are specifically designed to receive json and convert before setting a state.
I am writing the json from hass to backup my input_booleans and input_selects as state restore is not 100% reliable (see link below). By writing each profile in json i can backup my settings onto around 10 mqtt entities rather than more than 40.
I am trying to process this json so I can manually restore backed up states from the json backups rather than having to do it by hand in the front end
What you probably want is the ability within the MQTT sensor to parse JSON to multiple attributes. The REST sensor was recently edited to allow this (see “json_attributes” https://home-assistant.io/components/sensor.rest/)
As a stop gap you could parse your MQTT JSON to multiple sensors? value_json works within the mqtt sensor (Value from json (with points))
Another workaround for this is create a script that sets the input_booleans and input_selects to a value. I did this a few weeks ago because I was tired of having to reset everything in the event I had to hard restart HA and everything was turned off at default. I also raised a feature request a while back that asks initial_value to only be used when there was nothing in the database but didn’t get much response on it.