Trigger or condition: When value (CO2) is rising for two minutes - problem in closing window automation

Hi I am struggling with automation for closing the windows. Currently it is being triggered:

  1. If value from CO2 sensor is less than 800
  2. If temperature value is less than selected by input_number

Conditions:

  1. If value from CO2 sensor is less than 800

The CO2 value often get closed to 820 but from that starts to little bit rising but the temperature in room is droping terribly (it is winter) so it get me wake up.

Is there a possibillity to make a “or” condition that will check if the CO2 values is rissing or stays on the same value for a certain period of time so to make window close?

Could you please post your current automation?

Hi Phil!

It is:

- id: '1574877648214'
  alias: 'Okno: Nízká Teplota Zavři'
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.pokoj_temperature'')|float < states(''input_number.noc_teplota_zavreni_okna'')|float
      }}'
  - platform: template
    value_template: '{{ states(''weather.home'') == (''rainy'' or ''lightning-rainy''
      or ''snowy-rainy'' or ''pouring'' or ''hail'')}}'
  - below: '850'
    entity_id: sensor.co2
    platform: numeric_state
  condition:
  - condition: state
    entity_id: switch.okno
    state: 'on'
  - condition: state
    entity_id: binary_sensor.okno_fixed
    state: 'on'
  - below: '850'
    condition: numeric_state
    entity_id: sensor.co2
  action:
  - data:
      entity_id: switch.okno
    service: switch.turn_off

I believe you need a for: line in your below trigger.

  - below: '850'
    entity_id: sensor.co2
    platform: numeric_state
    for: '00:02:00'

This will trigger when the sensor.co2 has been above 850 and then goes BELOW 850 FOR 2 minutes.

In this specific case I do not want to set it below certain value, I need HA to recognize that the values is constant or rising, in that case to shot the trigger.

Before we get to your question, there is at least one problem with your current automation. This will not work the way you expect:

  - platform: template
    value_template: '{{ states(''weather.home'') == (''rainy'' or ''lightning-rainy''
      or ''snowy-rainy'' or ''pouring'' or ''hail'')}}'

The right side of the == operator will evaluate to 'rainy', so the statement will only be True if the state of weather.home is rainy.

Also, you should quote the entire template with double-quote characters to make using single-quote characters inside the template string easier. And actually, since these are long templates you should probably use multi-line YAML instead.

Lastly, there’s no point in quoting the below value for the numeric_state trigger.

So, you should change your triggers to:

  trigger:
  - platform: template
    value_template: >
      {{ states('sensor.pokoj_temperature')|float <
         states('input_number.noc_teplota_zavreni_okna')|float }}
  - platform: template
    value_template: >
      {{ states('weather.home') in
         ('rainy', 'lightning-rainy', 'snowy-rainy', 'pouring', 'hail') }}
  - below: 850
    entity_id: sensor.co2
    platform: numeric_state

Now getting to your question, I suppose there is probably a way to do what you ask for, but it gets a bit involved. Why not just remove the sensor.co2 condition and let the automation turn off the switch whenever any of the triggers fire (assuming the other conditions are met)?

Also, the BEST way to get ACCURATE help is to CLEARLY define EXACTLY what you want to happen. HA absolutely can accomplish what you want - but you need to know specifically what you want it to do.

I also leverage a sensor based on the ‘gradient’ attribute of a trend sensor for some automations.

Anyhow, if you’d like to spell out exactly the scenarios you want the blinds open / closed upon - we’re happy to help - but it’s difficult w/ inaccurate and/or missing details.

Thanks Phil! I am learning to code so I will double check the guide regarding the operators.

Regarding the qoutes I am also noob however on the value, these were added when I edited the automations in Hassio. I will also google for where and how to quote.

I believe that now I can make what I need by Trend Binary Sensor as @Markus99 pointed out. Thanks Mark!

I will post the result after. Thanks guys again for your help.

Do you have idea why the template is reformated when editing in hassio? Am I banned when using this kind of formating of templates from editing in Hassio? As I am a noob I did a quick way and added state trigger in Hassio causing the reformating. I fixed the code and now it looks like this.

Opening window

- id: '1574877574417'
  alias: 'Okno: Vysoká Teplota Otevři'
  description: ''
  trigger:
  - platform: template
    value_template: >
      {{ states('sensor.pokoj_temperature')|float > states('input_number.noc_teplota_otevreni_okna')|float }}
  - above: '1000'
    entity_id: sensor.co2
    platform: numeric_state
  - platform: template
    value_template: >
      {% set last_triggered = as_timestamp(state_attr('automation.okno_vysoka_teplota_otevri', 'last_triggered')) %}
      {{ last_triggered is none or (as_timestamp(now()) - last_triggered) > 60*60 }}
  condition:
  - condition: state
    entity_id: switch.okno
    state: 'off'
  - condition: state
    entity_id: binary_sensor.okno_fixed
    state: 'on'
  - condition: template
    value_template: >
      {{ states('weather.home') not in
         ('rainy', 'lightning-rainy', 'snowy-rainy', 'pouring', 'hail') }}
  action:
  - data:
      entity_id: switch.okno
    service: switch.turn_on

Closing window

- id: '1574877648214'
  alias: 'Okno: Nízká Teplota Zavři'
  description: ''
  trigger:
  - platform: template
    value_template: >
      {{ states('sensor.pokoj_temperature')|float <
         states('input_number.noc_teplota_zavreni_okna')|float }}
  - platform: template
    value_template: > 
      {{ states('weather.home') in
         ('rainy', 'lightning-rainy', 'snowy-rainy', 'pouring', 'hail') }}
  - below: '800'
    entity_id: sensor.co2
    platform: numeric_state
  - entity_id: binary_sensor.co2_trend
    platform: state
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.co2_trend
        state: 'on'
      - condition: and
        conditions:  
        - condition: state
          entity_id: switch.okno
          state: 'on'
        - condition: state
          entity_id: binary_sensor.okno_fixed
          state: 'on'
        - below: '800'
          condition: numeric_state
          entity_id: sensor.co2
  action:
  - data:
      entity_id: switch.okno
    service: switch.turn_off

If by “editing in hassio” you mean using the Automation Editor in the UI, then you’re not “banned” from also directly editing the YAML files directly. But you need to be aware that the Automation Editor has its own peculiarities in the way it likes to order & format things, and doesn’t support every feature you can use by directly editing the YAML files. You should probably use one method or the other (i.e., use the Automation Editor, or directly edit YAML files), but not both, at least until you understand things more.

I would say most “advanced users” don’t use the Automation Editor, mainly because it has been very limited for most of its existence. However, I know a lot of work has been done recently to improve it. But I think it has a ways to go yet to be “fully functional.”

Guys, thank you for all the tips! It will need some tweaks to work on the automation but it seems it is almost working great! :slight_smile: