Automation using template comparing 2 sensors values

Greetings,

I’m quite new in HA world as I started in may 2021.
I have a Pi where HA OS is running up to date. I am using ZigBee2MQTT and paired 18 devices (temperature/humidity, switches, TRV, motion sensors…).
I managed to create several automations using HA integration.

But now, I am facing a little pain in my a## as I can’t manage how to make what I want to work.

I am willing to trigger a switch using a template condition by comparing sensor 1 and sensor 2 like sensore 1 > sensor 2.

I know it may be very trivial for you but I am not a programmer and only start to understand what I am doing.
I used the developer tool to check the template and it is working. But I may forget something to make it work in an automation.

Could you please help me to understand what is missing ?

Here is the code I use

alias: Test template VMC humidité
trigger:
  - type: humidity
    platform: device
    device_id: 513957de5970cf97115e99543f81
    entity_id: sensor.sonde_salon_humidity
    domain: sensor
    above: 70
    for:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
condition:
  - condition: template
    value_template: >-
      {{states('sensor.sonde_sdb_bas_humidity') <
      states('sensor.sonde_salon_humidity')}}
action:
  - type: turn_off
    device_id: 5dca81ec152cf3864ecd29028d0c
    entity_id: switch.switch_vmc_bas
    domain: switch
  - device_id: d151214fd1edf592f9d5ab4716bc
    domain: mobile_app
    type: notify
    title: Test template VMC humidité
    message: TEST VMC 70% salon réussi !
mode: restart

I must miss something on the template like I should add that if it is true, then proceed to action…

Thanks a lot

Bud

Can you please share the error message that appears at your HASS log when you manually execute your automation?

Hi Rokam,

There is no error in HASS log…
When I click on execute, it is going as normal.
But when it is left on auto, it is just doing nothing even if i’d like it to !

Is there any info appearing inside the automation tracing?

If you start the automation “by hand” eg. in the template editor, the trigger will not be checked.

I wouldn’t use the device platform, this is the perfect use case for a numeric_state trigger. I’m not a fan of the device trigger, makes things more complicated than needed, imho. :slight_smile:

That would be a trigger like this:

trigger:
  - platform: numeric_state
    entity_id: sensor.sonde_salon_humidity
    above: 70
    for:
      minutes: 5

there is nothing related to error in the tracing. It seems it is avoiding the condition to execute the automation

Change it to this:

    value_template: >-
      {{ states('sensor.sonde_sdb_bas_humidity') | float <
         states('sensor.sonde_salon_humidity') | float }}

You have to convert the entity’s state value to a numeric value using either int or float. If you don’t do that, it is comparing the two values as strings (text) which can produce unexpected results.

You should also be aware of how the trigger works (it’s a Numeric Device Trigger which is effectively the same as a Numeric State Trigger). It will trigger when the humidity increases from below 70 to above 70 and remains above 70 for at least 5 minutes. Once it triggers, it won’t trigger again until the humidity first decreases below 70 and then increases above it.

If you created the automation when the humidity was already above 70, the automation will not trigger. The humidity must first decrease below 70 and then increase above it to trigger the automation (or restart Home Assistant or reload automations).

2 Likes

Hi Paddy,

Thanks for the advice. I’m changing that right now.
I used all my other automations using device :slight_smile:

Hi 123,
Thanks for the explaination on the “float”. I didn’t know that I’d need that.
What is the difference between float and int ?

I know how work the automations. That’s why I am lowering the value to above 50 which is the case now.

int converts the value to an integer.
float converts the value to a floating point number.

If the sensors report whole numbers only (1, 12, 556, etc) then use int otherwise use float.

Guys,

Thanks a lot for your help ! it’s working now !
To test, I just had to restart the server. When it starts, the automation is launched.
Tried with > to verify it was not an artefact :wink:

Thank you very much !!!

Super !!! That will be very useful for me and probably for other readers like me :smiley: !

2 Likes

To be clear, there was nothing wrong with the automation’s trigger. Changing it from a Numeric Device Trigger to a Numeric State Trigger reduces the number of lines of code but doesn’t change the behavior (two ways of creating the same kind of trigger).

Reducing the Numeric Trigger’s threshold value and then reloading automations serves to reset the trigger.

What was definitely incorrect was arithmetically comparing two state values without first converting them to numeric values.

Well thank you for your help ! I was not far from the goal but I would have missed this important part !
For me it is time to sleep ! I juste have to wish you a pleasant afternoon :slight_smile:

See ya

Great it’s working! :+1:

If you wouldn’t mind, please mark the post from Taras as solution (the checkbox under his first post). Helps others to see the solution at first glance! Thanks! :slight_smile:

And a pleasant afternoon to you, too from the balcony looking in the night sky. :smiley:

yes !!! I already checked the box as solution ! :slight_smile:

Taras,
I don’t know if I can add another question in the same topic but it is related.
I was wondering if it was possible to make some kind of mathematic operations like :

{{ states('sensor.sonde_sdb_bas_humidity') | float < states('sensor.sonde_salon_humidity') +2 | float }}

Thanks :slight_smile:

Yes but the example you posted contains a small error.

states('sensor.sonde_salon_humidity') +2 | float
                                       ^
                                       |
   You cannot add 2 to the sensor's value here.

You can add 2 to the sensor’s value after it has been converted.

states('sensor.sonde_salon_humidity') | float + 2

Here’s your corrected template:

{{ states('sensor.sonde_sdb_bas_humidity') | float < states('sensor.sonde_salon_humidity') | float + 2 }}

1 Like

Super ! Thanks :slight_smile: I just figured it out using the forum :slightly_smiling_face:

Thanks again ! Hope that one day, I will be able to help others here !

1 Like

Hello,
I intend to do something similar also to automate a VMC unit based on humidity of a bathroom, but I want that the trigger occurs based on value from two sensors. One is the humidity’s mean of last 2hr, and the other is the actual humidity.
I already created the statistics sensor to get humidity’s mean.

I’m using Triggers type - Template, and this is the code:

platform: template
value_template: |-
  {{ states('sensor.humidade_wc_last_hr.attributes.mean') | float <
     states('sensor.bme_3_relative_humidity') | float }}

I’m getting this error: “Message malformed: Integration ‘’ not found”

Thanks