Run automation after sensor value exceeds value for second time

I have a CO2 sensor which triggers my ventilation to start if CO2 levels are above some value. I would like to adjust this automation to start the ventilation only after a second time CO2 levels are above this value. How to implement that?

What I have now:
A value-template to set the CO2-alarm state:

    co2_1_alarm:
        value_template: >-
            {% if states('sensor.co2_1')|int < 1000 %} green
            {% elif states('sensor.co2_1')|int >= 1000 %} orange

And an automation to trigger the ventilation based on this CO2-alarm-state:

trigger:
  - platform: state
    entity_id: sensor.co2_1_alarm
    from: green
    to: orange
condition: []
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.blitz_03_power
mode: single

The CO2 leves are reported via MQTT every 2 minutes. For now the ventilator is turned on/off by switching the power of the ventilator but this will be changed to multiple speed settings.

Try this (be advised it is untested). It uses the new wait_for_trigger feature. In theory, the first time the sensor changes from green to orange, it will trigger the automation. The automation then waits for the sensor to change from green to orange again before turning on the switch.

alias: Example 1
trigger:
  - platform: state
    entity_id: sensor.co2_1_alarm
    from: green
    to: orange
condition: []
action:
  - wait_for_trigger:
    - platform: state
      entity_id: sensor.co2_1_alarm
      from: green
      to: orange
  - service: switch.turn_on
    data: {}
    entity_id: switch.blitz_03_power
mode: single

You mean the value is say 1005, then the next is 1009.
That is a trigger but if you have 1005, 999, 1003 then you do not want to trigger?

I don’t think that will work since the sensor will not change for a second time to orange…!?!

Correct only trigger if two sebsequential values exceed the trigger value of 1000.

If you use the statistics sensor and set the sampling size to 2, then you can look at the attribute max value and min value.
If both are above 1000 then trigger.

sensor:
  - platform: statistics
    entity_id: sensor.co2_1
    sampling_size: 2

and:

    co2_1_alarm:
        value_template: >-
            {% if state_attr('sensor.statistics', 'max value')|int < 1000 and state_attr('sensor.statistics', 'min value')|int < 1000  %} green
            {% elif state_attr('sensor.statistics', 'max value')|int >= 1000 and state_attr('sensor.statistics', 'min value')|int >= 1000 %} orange
1 Like

Perhaps I’ve misunderstood what you are trying to achieve. I interpreted this:

start the ventilation only after a second time CO2 levels are above this value

to mean you want the automation to trigger when sensor.co2_1_alarm changes state to orange the second time. It only changes state to orange when sensor.co2_1 exceeds 1000 and you want to know when that happens twice in a row.

If you want it to run the automation only if the sensor reads “orange” for two consecutive readings and the sensor gets updated every two minutes then it’s easy enough to just add a “for:” in the trigger:

trigger:
  - platform: state
    entity_id: sensor.co2_1_alarm
    from: green
    to: orange
    for:
      minutes: 5 # to make sure to exceed the max update interval for two updates
condition: []
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.blitz_03_power
mode: single

The Statistics sensor does the job and is a very nice sensor to know for other puposes as well. Thanks!

Maybe I should have been more clear, I want the trigger to be ran after two sensor readings above 1000 in a row. So 1010-1030 reading should trigger, whiile 1010-900-1030 should not trigger. The Statistics sensor mentioned above does the trick.

1 Like

This is not what I want since the alarm will not change from green to orange two times if the readings are above 1000 two times in a row.

It doesn’t need to, finity looks at the state for the required period

You’re right. But it doesn’t need to as Mutt said.

As long as you get the end result you want that’s the important thing but I always like to make things easier rather than harder. Having to create a second sensor is definitely harder than just adding a bit of code to the existing automation.

Hey there, sry for digging this out but I have something similar to solve and can not figure out how and where to place the alarm statement, can someone provide an example in yaml?

I am trying to see if a power level has dropped below a threshold for at least 15 min. max_age on statistics sensors seems to do the trick, but how do i get the state change from the value_template implemented? It seems that there is context missing which i can not generate.

1 Like