Automation is triggering when it should not

Hi,

Background: I have an ultrasonic sensor coded with ESPHome and it provides a regular stream of distances which I convert to litres in our home’s water tanks. The sensor ‘sensor.use_today’ subtracts the current tank value from yesterday’s value (which is set as an input_number value at midnight). I have a statistics sensor that compares the last 7 input_number values and averages them - named sensor.seven_days_water_use.

Anyway, that is all working fine and as you can see from the history here. It’s not perfect due to the variation in values from the sensor but it’s close enough for my needs.

The problem with all this is my automation. We have just entered summer and we likely won’t get much rain so we need to be careful with our water usage in the next few months. I made an automation to notify me when our use_today is greater than our average seven_days_water_use. The template editor suggests that this template works, yet it’s triggering all the time. It usually triggers within a couple of minutes of midnight. When I made it and didn’t have it trigger once per day (by disabling the automation until midnight) it triggered every few minutes even though it was well under the average.

Automation:

id: '1670374497438'
alias: Water Use Warning
description: >-
  If you've used more than the 7 day average and the date is less than 15th
  April 2023 notify about water use. You'll need to update the epoch date for
  2024 - 1713108690, 2025 - 1744644690, 2026 - 1776180690
trigger:
  - platform: template
    value_template: |
      {{ states('sensor.use_today') > states('sensor.seven_days_water_use') }} 

       
condition:
  - condition: template
    value_template: '{{((as_timestamp(states.sensor.date_empty.state)))|int < 1681485613}}'
  - condition: template
    value_template: '  {{ states.sensor.use_today.state > states.sensor.seven_days_water_use.state }}'
action:
  - service: notify.notify
    continue_on_error: true
    data:
      title: Water Alert
      message: >-
        Be careful with water use. {{ states('sensor.use_today') }} litres used
        today. At this rate, we will run out of water by {{
        states('sensor.date_empty') }}. It probably won't rain properly until
        mid-April.
  - service: automation.turn_off
    data: {}
    target:
      entity_id: automation.water_use_warning
mode: single

Can anyone help?

You need to convert the values in your trigger template to float or int, otherwise you’re comparing strings.

{{ states('sensor.use_today') | float(0) > states('sensor.seven_days_water_use') | float(0) }}

You also don’t need the second condition.

1 Like

I agree this is better but on-the-side from my view/experience, the compare also works if within the there are numbers between the quotes, i.e.

{{ '0.2' > '0.1' }} will show true too

. Are you sure that you are always comparing numbers? Might it be that the sensor goes offline = unavailable ?

1 Like

That only coincidentally works because of how Python compares strings. Try this in developer tools > template and you’ll notice how it also returns True despite 2 obviously being less than 100:

{{ '2' > '100' }}
2 Likes

I dont have any feedback regarding to automation triggering but i have a question about formula.

First of all, great setup, just congrats

You are taking last 7 day’s average and if today’s consumption is greater, it is an alarming state. But, average means average of values, so if you never go above average z your average will go lower and lower and next day you need to use less. Everyday, you are required to use less than previous 7 days average and this will approach to 0.

I would compare to average but also highest of last 7 days, or something else. This is just stuck in my mind and I wanted to share.

1 Like

This is next level and blows my mind. You’ve taught me not to compare strings and to compare numbers which will help avoid future problems. Thanks for your prompt answer!

1 Like

Thanks fuatakgun!

I see what you’re saying and I think I understand. This is where the condition comes in which I didn’t explain. The goal of this automation is to get through to mid-April without having to buy water. So the automation will stop running once sensor.date_empty (which is the total water in storage divided by the sensor.seven_days_water_use value, converted to a date) is greater than 14th April 2023.

1 Like