Extract a series of date based values from JSON

Hoping someone can help me unpick this issue.

Have a file sensor which contains dates for next rubbish collection. Template editor defines it as a dict, so I believe it’s being parsed correctly.

  "garbage": "21/12/2021",
  "recycling": "21/12/2021",
  "green_waste": "28/12/2021"

Sensor is called rubbish_collection so when I put {{ states.sensor.rubbish_collection.state }} in the template editor it’s showing correctly. Trying to get the values of each of the attributes in the object so I can test which one is coming up next and show something different in Lovelace depending. But basic tests aren’t working.

Tried everything

{{ states.sensor.rubbish_collection.state.garbage }}
{{ states.sensor.rubbish_collection.state['garbage'] }}
{{ states.sensor.rubbish_collection.state[garbage] }}
{{ states.sensor.rubbish_collection.state.[garbage] }}

Returns blank or error

{% set value_json = states.sensor.rubbish_collection.state %}
{{ value_json.garbage }}
{{ value_json.[garbage] }}

Interestingly when I do

{% set value_json = states.sensor.rubbish_collection.state %}
{{ value_json[1] }}

I get a single ’ back, type String. Does this mean it’s not parsing correctly. Checked the data file and it’s correctly marked as ".

Still quite new to objects/dict in Python.

All states are strings, they aren’t objects so none of your attempts will work unless you convert it to the object.

{% set value_json = states('rubbish_collection.state') | from_json %}
{{ value_json.garbage }}

Thanks very much, I think there is something wrong with the data so I will investigate what’s happened. JSON validates as well formed, but perhaps there is a character I can’t see.

Doing as you said threw this: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Config file has the following:

- platform: file
  name: rubbish_collection
  file_path: /home/data/integrated_component/rubbish_collection/data.json
  value_template: '{{ value_json }}'
  scan_interval: 21600 # 6 hours

Also tried without the value_template: '{{ value_json }}' line, same issue.

Thanks very much for this I had the sensor call misconfigured due to not understanding the format. For those in the future having a similar issue
{"garbage":"28/12/2021","recycling":"5/1/2022","green_waste":"28/12/2021"} would have the following

{% set value_json = (states("sensor.rubbish_collection") | from_json()) %}
{{ value_json.garbage }}