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
sensors:
cinema_cineworld_sun_film:
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?