[solved] Sensor Template not calculating at certain times?

Hi there;
I have two Hue Motion sensors outside I use as temperature sensors.

“Driveway” is mostly correct except for early mornings where it heats up from sunlight.

“Front door” is usually warmer then real temp from the roof, but is more accurate in the morning when “driveway” is getting warm in the sun.

I wish to make a simple sensor template that creates a sensor “outside lowest”, which, takes the temperature of either “Driveway” or “Front Door” , whichever is the lowest.

The template appears to work. Except between roughly 0730h - 0930h in the morning!! Of which, the logic maths doesn’t work, and there is a sudden abrapt change from “front door” (coolest at that time) to “Driveway” (about 10-15 degrees warmer).
Then randomly, it switches back, and behaves for the rest of the day.

Why is it doing this? HA overloading at that time? I can’t for the life of me work out why.

Here is the Sensor Template in Config.yaml;

  - platform: template
    sensors:
      outside_temp_lowest:
        friendly_name: 'Outside Temp'
        unit_of_measurement: '°C'
        value_template: >
          {% if states('sensor.front_door_sensor_temperature') > states('sensor.driveway_sensor_temperature') %}
            {{ states('sensor.driveway_sensor_temperature') }}
          {% else -%}
            {{ states('sensor.front_door_sensor_temperature') }}
          {% endif %}

Graph picture attached of the temp jumping up to Driveway and back down to front door.

Any help much appreciated.!

Screenshot from 2020-12-20 19-56-59

Did you try the min/max sensor yet?

That one will take the lowest ever recorded no?
I want the lowest of the two at that exact moment - ie; just an accurate temperature reading.

No, it takes the lowest from the latest value of all the sensors specified

Change your template to this:

        value_template: >
          {% set front = states('sensor.front_door_sensor_temperature') | float %}
          {% set driveway = states('sensor.driveway_sensor_temperature') | float %}
          {{ driveway if front > driveway else front }}

The state value of all entities in Home Assistant is a string. When your template compares the two sensor values like this:

          {% if states('sensor.front_door_sensor_temperature') > states('sensor.driveway_sensor_temperature') %}

it’s not comparing two numbers, it’s comparing two strings.

Comparing two numeric strings is not like comparing two numbers. It can produce erroneous results as demonstrated in the following screenshot. The first template compares two numeric strings (and concludes '10' is not greater than '9') whereas the second template compares two numbers (and comes to the correct conclusion).

String vs Numeric Comparisons

Hi Burning Stone;
You’re right! Thanks so much, I implemented it, and it works perfectly.

Hi Taras;
Thanks for explaining why they weren’t working, I have a lot more template sensors I wish to make so I can use them for other things.

Thank you both very much! Have a good Holiday & stay safe!

1 Like