Template sensor not updating

I’ve been trying all sorts of things to set a template sensor successfully, and am at the end of my tether. I currently have two sensors set up that contain the info for the weekend “juniors” films at my local cinemas. Thee get populated by two Python scripts, and I end up with each one having a state that is a json object of the films and times.

From this I’m trying to get (for now) another entity to populate using a template sensor, so that I can pull the info out into a card on the front end. My problem is that the scripts take a few seconds to run, and the template sensor fires first and gives back an “Unknown” value. Here the info that I have for the template sensor:

  - platform: template
        value_template: '{% if states.sensor.cinema__cineworld_juniors %} {{ cinema__cineworld_juniors["cineworld"]["sun"]["1"]["name"] }} {% endif %}'
        friendly_name: Cineworld Sun Film

Note that the {% if clause is new, in the hope that it would not fire until the main entity was populated.

Here’s an example of the json object:

{"cineworld": {"sat": {}, "sun": {"1": {"name": "Ferdinand", "time": "10:00"}, "2": {"name": "The Nut Job 2: Nutty By Nature", "time": "10:10"}}}}

And the info from the logs:

2018-03-11 19:57:30 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.cinema_empire_sun_film, old_state=None, new_state=<state sensor.cinema_empire_sun_film=unknown; friendly_name=Cineworld Sun Film @ 2018-03-11T19:57:30.026761+00:00>>
2018-03-11 19:57:33 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.cinema__cineworld_juniors, old_state=None, new_state=<state sensor.cinema__cineworld_juniors={"cineworld": {"sat": {}, "sun": {"1": {"name": "Ferdinand", "time": "10:00"}, "2": {"name": "The Nut Job 2: Nutty By Nature", "time": "10:10"}}}}; friendly_name=Cinema - Cineworld Juniors @ 2018-03-11T19:57:33.015868+00:00>>
2018-03-11 19:57:35 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.cinema__empire_juniors, old_state=None, new_state=<state sensor.cinema__empire_juniors={"empire": {"sat": {}, "sun": {"1": {"name": "Ferdinand", "time": "10:00"}, "2": {"name": "No film name available", "time": "10:10"}}}}; friendly_name=Cinema - Empire Juniors @ 2018-03-11T19:57:35.382883+00:00>>

The Python scripts run about once a minute, but the template never gets updated.

Is there any way of getting this to recognise that the main entities have been populated and then updates with the right info?

Hmm, just been looking at other ways of doing what I want, i.e. a card on the front end with cinema times, and found this thread which may be an alternative route to go down: Display only text in card

Shouldn’t you be accessing the actual state?!? I believe it should be:


{{ states.sensor.cinema__cineworld_juniors.attributes.cineworld.sun.1.name }}

use the template editor to see it

Thanks both for your advice.

@mvdkleijn you were correct in that in order to get just the state (rather than all of the other properties as well) I need to add .state at the end.

@petro the template editor was working when I set a variable to my JSON object, with square brackets, but it also works with the dot notation (although one needs to put digits within [’’]) which was something I wasn’t aware of, thanks.

After all this, I now see what the problem is.

I’ve got a new sensor, sensor.empire_data with a state of {"empire": {"sat": {"1": {"name": "No film name available", "time": "10:10"}, "2": {"name": "Gnomeo and Juliet", "time": "10:20"}}, "sun": {"1": {"name": "No film name available", "time": "10:10"}, "2": {"name": "Gnomeo and Juliet", "time": "10:20"}}}}, which was created using the template sensor.

The problem seems to be that HA doesn’t consider this a JSON object. Testing via the Template editor I can retrieve what looks like a JSON object using {{ states.sensor.empire_data.state }}

However, using {{ states.sensor.empire_data.state.empire.sat }} (or the [] notation) gives me the error:

Error rendering template: UndefinedError: 'str object' has no attribute 'empire'

So, it seems to me that, even though I’ve populated that state using a value_template, HA considers it a string, rather than a JSON object, and as such I don’t have access to the values. This seems contradictory to the info on https://home-assistant.io/docs/configuration/templating/ (at the bottom, under “Processing incoming data”) and I now have now idea how to get it to treat it as JSOn instead of a string.

Just to be clear, the template sensor code that works is this:

  - platform: template
        value_template: "{{ states('sensor.cinema__empire_juniors') }}"
        friendly_name: Empire

and it gives me a state of

{"empire": {"sat": {"1": {"name": "No film name available", "time": "10:10"}, "2": {"name": "Gnomeo and Juliet", "time": "10:20"}}, "sun": {"1": {"name": "No film name available", "time": "10:10"}, "2": {"name": "Gnomeo and Juliet", "time": "10:20"}}}}

Any more ideas from anyone?

Can I ask where you’re getting this sensor.cinema__cineworld_juniors ? is this a component?