Automation using template comparing 2 sensors values

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

Hi Andre,
Does your sensor.humidade_wc_last_hr.attributes.mean works ? when you use the developper tool do you have a value using

{{ states('sensor.humidade_wc_last_hr.attributes.mean') | float}}

?
Cause this should work

Hello budcalloway,

Actually in developer tools I’m not getting data.

How can I use this atribute value in Automation?

image

Thanks

What do you have in the “States” tab ?

You have the format mixed up… either of these two will work (the top one is preferred):

{{ state_attr(‘sensor.humidade_wc_last_hr’, ‘mean’) | float(0) }}

{{ states.sensor.humidade_wc_last_hr.attributes.mean | float(0) }}

Hello Didgeridrew,

Thanks a lot for the help.
Now I’m struggling to make the automation work.

I intent to use Automation Template Trigger with two sensors like this:

{% if state_attr('sensor.humidade_wc_last_hr', 'mean') | float(0) < states('sensor.bme_3_relative_humidity') | float(0) %}true{% endif %}

But it seems that is not actually working, in dev tool-template, this is what I get:

Thanks

Your template is working correctly. The template is false and you did not tell it what to do in that case, so it returned nothing…

For a your purposes you don’t need an if statement… all you should need is:

{{ state_attr('sensor.humidade_wc_last_hr', 'mean') | float(0) < states('sensor.bme_3_relative_humidity') | float(0) }}

Thanks a lot Didgeridrew.

It’s finally working.
So now if the mean humidity of last 2 hours (plus 10%) is less that actual humidify it will trigger the VMC to level 3.

- id: '1637154302262'
  alias: WC - Humidade VMC nível 3
  description: se humidade atual acima humidade média+10%, vmc nível 3
  trigger:
  - platform: template
    value_template: '{{ state_attr('sensor.humidade_wc_last_hr', 'mean') 
      | float(0) * 1.1 < states('sensor.bme_3_relative_humidity') | float(0) }}'
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: ventilation/ventset
      payload: '3'
      qos: '2'
  - service: notify.mobile_app_mi_a2
    data:
      data:
        ttl: 0
        priority: high
      message: WC Hum VMC n3
  mode: single
2 Likes

Hello again Didgeridrew,

This is a little bit off topic, but maybe you can guide me on how to create a sensor to get the value of 24hr ago.

Tks

Hi everyone!

Can someone help me with the templates below?
Both value templates give me an error when I test them.

This is the error I get:

image

action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.multisensor_basement_humidity') | float >
              states('input_number.basement_max_humidity') | float }}
        sequence:
          - service: humidifier.set_mode
            data:
              mode: Dry
            target:
              entity_id: humidifier.dehumidifier_bsmnt
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.multisensor_basement_humidity') | float <=
              states('input_number.basement_max_humidity') | float }}

I don’t understand what is wrong with the templates. Anyone sees the mistake?

Thank you!

Your float filters will need defaults. However, the error is due to the fact that the UI condition tester in the automation editior does not currently work for Template conditions.

1 Like

Didgeridrew is right.
I’d add that if you want to test your templates, do it in the dev tools