Hi folks, I’m wondering if I can have some assistance with this sensor template. I am running Hass.io 0.81.6 on a Raspberry Pi 3. The sensors are Xiaomi Aqara temperature, humidity and pressure sensors (the new square ones) and I am using Zigbee2MQTT with no Xiaomi gateway. All is working well with the sensors reporting in. I have successfully created this template which shows the temperature as expected. This is verbatim from my configuration.yaml…
sensor:
- platform: template
sensors:
temp_test:
friendly_name: Test Test
value_template: '{{ states("sensor.0x00158d00027263ee_temperature") }}'
I should add that I have to use the states function because the below works in the template developer but returns unknown after a reboot. Hassio doesn’t like the “0x” in the sensor name which is the reason for the square brackets.
I have no sensors starting with a number, but understand your need to use the states() notation. So it would be worth the try to substitute states('sensor["0x00158d000272b4f7_temperature"]') in the above template.
In my setting this renders unknown… so it should be syntactically correct
Thanks for your help all. I upgraded hass.io to 0.84.6 and it all works now. Must have been a bug in 0.81.6. For reference the syntax in this line works for me…
Thanks for that. I will be improving on my code layout now that I have a working baseline and will put your multi line templates suggestion into practice.
Here is a sensor template I have declared in my sensors.yaml file that might help with syntax. sensor.outside_temperature is a Xiaomi Aqara temperature and humidity sensor and I’m pretty sure the below code also worked before I renamed the sensors to something more sensible from the Xiaomi default which is the same format as your sensor names. The sensor is a differential between inside and outside temperatures I use to make ventilation decisions. Hope that helps.
of course that’s only pertaining to the last_changed . Now given the fact the object_id starts with a number, you have to follow the instructions Finity links to, and bracket the object_id. Since I don’t have object_id’s like that I can’t say for sure, but you should try:
Thank you all for your replies!!
Got all my sensors with 60 seconds delay working again.
Very usefull for alarm triggers that use an interval (pending) before actually triggering the alarm (or sending notification).
In my notification I list the sensors that have changed status in the past 60 seconds. So I know which door/window triggered the alarm.
binary_sensor:
- platform: template
sensors:
door_window_sensor_158d00019ff242_last60s:
device_class: door
friendly_name: Voordeur Sensor Last 60 Seconds
value_template: "
{% if states.binary_sensor['0x00158d00019ff242_contact'].last_changed is not none %}
{{ ( as_timestamp(now()) - as_timestamp(states.binary_sensor['0x00158d00019ff242_contact'].last_changed) ) < 60 }}
{% else %}
False
{% endif %}"
glad you got it working, I think the template can be shortened to:
{{ states.binary_sensor['0x00158d00019ff242_contact'].last_changed is not none and
(as_timestamp(now()) - as_timestamp(states.binary_sensor['0x00158d00019ff242_contact'].last_changed) ) < 60 }}
and even shorter:
{% set contact = state.binary_sensor['0x00158d00019ff242_contact'].last_changed %}
{{ contact is not none and
(now() - contact).total_seconds() < 60 }}