Hi all, not a first timer but still feeling new all the while!
Hoping I can get some help for this as it bugs me that I can’t seem to get a template entity working the way it should be but the template not throwing up any errors in the Jinja Editor in HA.
So this is my template that I am trying to get my head around;
Could not render template Last opened, the state is unknown.
5:41 PM components/template/sensor.py (WARNING)
This also shows a ‘unknown’ state on the dev state page and yet if I put the template code into the editor it shows correctly the last opened entity.
I have added the front door sensor (and will add more as I get more sensors) into a few groups: exit_doors, entrance_doors and alarm_sensors. This has still not changed nor updated the state of the entity.
I will freely admit that I got this part of my presence detection code from @Tinkerer on here, I have asked the question however his jinja templating skills are about as abundant as mine!
Really love the power of templating, and wish I knew more and it seems that I am learning as I go along however this time I’m pretty stumped as my template results show I’m getting no where fast!
Thanks for any help in advance - I will owe you a coffee/beer/smoke/can of water(no plastic!)
Weird, if it works in the dev-templates menu, it should work in the sensor.
Throwing an error on startup is normal, but if you open the front door (or manually change the state of the entity) does it update?
I am, however, moving towards using an input_text and an automation instead of a sensor. This is because every change to the sensor requires a restart of HA, and that’s a pain (partly because I’m on a Pi, partly because every so often, OZW corrupts it’s own cache file, and crashes HA on startup).
Well, I have mine in a Ubuntu Docker and restarting HA takes seconds as it runs on a SSD so this wouldn’t be a problem but I really don’t want it restarting everytime.
Can I ask @Tinkerer , what should be the output of this template (just to make sure what I get back from the editor is the correct result required for the automation to work)
That’s checking to see if this sensor (you can replace sensor and sensors with kipper and whales in that template FYI, they’re just variables) is the one with the most recent update. If it is, then it displays the name (see here) of the entity.
last_changed contains a datetime object. You can compare datetime objects without having to first convert them into timestamps. Therefore the template can be simplified to this:
value_template: >
{% set sensors = [states.binary_sensor.front_door] %}
{% set last = sensors | map(attribute='last_changed') | max %}
{% for sensor in sensors %}
{% if sensor.last_changed == last %}
{{ sensor.name }}
{% endif %}
{% endfor %}
I assume you will be putting more entities into the sensor list and you stripped it down to just one item for demo purposes?
So I currently have just the one sensor so currently I only want to use it for the one but for sure soon enough (like I’m ordering two more sensors now) I will be leaving it as is - so as a list of sensors.
Ok, so now I see the reason for the error kicking up - so how would I template this for the one sensor to work temporary for now? Having this for future use demo purposes would be useful to document too.
I have left the original and unused entities in there just commented them out, I don’t think this can cause issue. Give HA a reboot, opened and closed the door a few times and still no change in the error on HA and entity state?
Here’s my theory: This sounds like a case of unfortunate timing. After rebooting, Home Assistant creates the Template Sensor beforebinary_sensor.front_door contains a valid state.
Whatever device you are using to monitor the front door appears to take time to be detected by Home Assistant. While it is in the process of being initialized, the binary_sensor has no known state.
In contrast, the Template Sensor is created instantly, apparently well before binary_sensor.front_door. That presents a problem because the Template Sensor’s template depends on the availability of binary_sensor.front_door.
If I was faced with this situation, I would use the approach suggested by Tinkerer. Use an automation to monitor the security (door/window) sensors. Whichever sensor triggers the automation gets its friendly_name recorded in an input_text.
If you don’t like using an input_text for this purpose, the alternative is to use a sensor. However, there is no sensor.set_value service in Home Assistant. That makes it challenging for the automation to assign a value to a sensor.
I know of two workarounds:
If you’re already using MQTT, the automation can use mqtt.publish to set the sensor’s state.
@rodpayne has created a python_script, called set_state.py, that can set a sensor’s state. The automation can use python_script.set_state to set the sensor’s state.
I’ve used the first workaround so I can speak from experience about it. The sensor would look something like this: