Hassio problem with sensor templates - Solved

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.

value_template: '{{ states.sensor["0x00158d000272b4f7_temperature"].state }}'   

However this configuration and many variations of it does not work even though it works as expected in the Hass.io template designer…

sensor:
  - platform: template
    sensors:
      temp_diff:
        friendly_name: Temp Diff
        value_template: '{{ states("sensor.0x00158d000272b4f7_temperature") | float - states("sensor.0x00158d00027263ee_temperature") | float | round(2) }}' 
        icon_template: "mdi:thermometer"
        unit_of_measurement: '°C'

Any help would be much appreciated!

Put the double quotes outside and the single quotes inside the template.

Thanks Tom, unfortunately I get the same result when I switched the quotes around like below.

value_template: "{{ states('sensor.0x00158d000272b4f7_temperature') | float - states('sensor.0x00158d00027263ee_temperature') | float | round(2) }}"

Although I notice that clicking on the sensor now shows 2 temperature graphs like below

Image%20202

As opposed to another test sensor that shows only one like this;

Image%20203

What happens if you go back to using square brackets for the sensors?

How about using the config here for the device WSDCGQ11LM

https://koenkk.github.io/zigbee2mqtt/integration/home_assistant.html

if value_template: '{{ states.sensor["0x00158d000272b4f7_temperature"].state }}'

works for you in HA (and I mean the real Ha frontend, not the dev-template)

you could try to use the multiline notation to prevent issues with quotes, and use only single quotes inside the template:

value_template: >
  {{ states.sensor['0x00158d000272b4f7_temperature'].state }}

if that works, you could try:

value_template: >
{% set temp = states.sensor['0x00158d000272b4f7_temperature'].state %}
{{ temp | float }}

if that still works, you could try and add both sensors:

{% set temp_1 = states.sensor['0x00158d000272b4f7_temperature'].state %}
{% set temp_2 = states.sensor['0x00158d00027263ee_temperature'].state %}
{{ (temp_1 | float - temp_2 | float) |round(2) }}

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 :wink:

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…

value_template: "{{ (states('sensor.0x00158d000272b4f7_temperature') |float - states('sensor.0x00158d00027263ee_temperature') |float) |round(1) }}"

cool, but most surprising…

as always work with what works but just be ware of the note on this page:

and my suggestion to use multiline still holds, makes it all much simpler and less error prone, easier to debug and what have you. cheers!

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.

I thought based on this docco that it was preferred to use states() function rather than states.entity.state, so not sure what is best practice…
https://developers.home-assistant.io/docs/en/documentation_standards.html#templates

yes that’s what you want, to prevent errors from an entity not yet initialized. This way your templates always return a valid outcome.

do the same for attributes:

    {{state_attr('sensor.corridor_motion_sensor','battery')}}

just remember when templating the last_changed this can’t be used, and you must use {{states.sensor.corridor_motion_sensor.last_changed}}

So how does one need to format when trying to template the last_changed attribute with an entity with 0x in its name?

The following don’t work:

 {{ states('binary_sensor.0x00158d00019da9f0_contact.last_changed') }}
 {{ states.binary_sensor.0x00158d00019da9f0_contact.last_changed }}
 {{ state_attr('binary_sensor.0x00158d00019da9f0_contact','last_changed') }}
 {{ state_attr('binary_sensor.0x00158d00019da9f0_contact',last_changed) }}

Hi Bram,

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.

- platform: template
  sensors:
    temp_diff:
      friendly_name: Temp Diff
      value_template: "{{ (states('sensor.outside_temperature') |float - states('sensor.bedroom_temperature') |float) |round(1) }}"
      unit_of_measurement: '°C'

see this link for info.

1 Like

you cant template the last_changed of a state(entity), other than using this style template

 {{ states.binary_sensor.object_id.last_changed }}

if that doesnt work, you either have a typo in your template, or something else is at hand, please show this in your template editor)

your should show like:

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:

{{ states.binary_sensor['0x00158d00019da9f0_contact'].last_changed }}

found one after all :wink: :

{{ states.automation['2v12_alarm'].last_changed }}

1 Like

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 %}"

Thanks again!

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 }}
1 Like