Trigger automation would need an AND condition

Good morning.
He is trying to create an automation that must be triggered:

  • when it’s 6 o’clock
  • when it is 6 pm and the temperature has been above 30 ° for at least 6 hours.
    So something like this:
trigger:
    - platform: time
      at: "06:00:00"
    - and:
        - platform: time
          at: "18:00:00"
        - platform: numeric_state
          entity_id: sensor.meteonetwork_temp
          above: 30
          for: "06:00:00"

  condition: []
  action:
    do somethings

It is known that in the trigger section the options are linked by the default OR operator and it does not manage other logical operators except under platform: template

Then I could modify the trigger, delegating the temperature control to the action section:

trigger:
    - platform: time
      at: "06:00:00"
    - platform: time
      at: "18:00:00"
  condition: []
  action:
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: "0"
          sequence:
             # do somethings
        - conditions:
           - and:
              - condition: trigger
                id: "1"
              - condition: numeric_state
                entity_id: sensor.meteonetwork_temp
                above: 30
                for: "06:00:00"
          sequence:
             # do somethings
[...]

But unfortunately condition: numeric_state does not require the use of the for command.
Again it would be possible to use the condition: template

But how do you write the template for:

              - condition: numeric_state
                entity_id: sensor.meteonetwork_temp
                above: 30
                for: "06:00:00"


Since there are … the perfect template (for the second automation trigger) that I would need would be the one that:

  • when it is 9 pm AND between the previous 1 pm and 6 pm the temperature has been above 30 °.

Thanks for your attention

Create a trigger-based template binary sensor that goes on when the temperature has been above 30° for six hours, regardless of the clock time, and goes off as soon as the temperature drops below 30° (or some hysteresis value).

Then your automation is triggered by 06:00 and 18:00, with a condition that if it is 18:00, the binary sensor must be on to proceed.

You can trigger at both times and than check for the temperature in the condition when it was triggered at 6pm.

trigger:
  - id: "am"
    platform: time
    at: "06:00:00"
  - id: "pm"
    platform: time
    at: "18:00:00"    
condition:        
  - condition: or
    conditions:
      - "{{ trigger.id == 'am' }}"
      - condition: numeric_state
        entity_id: sensor.meteonetwork_temp
        above: 30
        for: "06:00:00"    
action:
  do something

Thanks work!

Any chances for

I’m afraid I claimed victory too soon.
Evidently I hadn’t wired my brain yet.
Using the “for” clause causes an error even when used in the condition section

@Troon
Sorry for the delay in responding but… I had to figure out what you meant to me with your advice.
I jotted this down:

- platform: state
  binary_sensors:
    - trigger:
      - platform: numeric_state
        entity_id: sensor.meteonetwork_temp
        #value_template: "{{ template }}"
        above: 30
        for: "06:00:00"
      oggi_ha_fatto_caldo:
        friendly_name: "Oggi ha fatto caldo?"
        state: "{{ 'on' if ('trigger.idx', '0') else 'off' }}"

But it keeps giving me error.
Actually once done it would answer my initial question.
If I understand correctly, the “for” option does nothing but every x time (1 sec, 1 minute?) look in the history of the sensor measurements if in the previous 6 hours the temperature has ALWAYS been above 30°
Thinking about it, however, it seems to me a wrong path to follow.
My need is not:

  • as soon as the temperature exceeds 30° for 6 hours, it activates the alarm
    situation in which it would make sense to “listen” permanently on the temperature channel.
    My need is:
  • it’s 6 pm. In the previous 6 hours has the temperature always been above 30°?
    I don’t need real-time temperature control.
    So it should do the check only once, which it does every X time (1 sec, 1 minute?).
    Perhaps there is another more efficient solution.

To resolve independently I had also thought of scheduling the trigger at 18 but this is not feasible due to the impossibility of using the “and” operator.

Thank you all for the attention paid to my case.

template:
  - trigger:
      - platform: numeric_state
        entity_id: sensor.meteonetwork_temp
        above: 30
        for: "06:00:00"
      - platform: numeric_state
        entity_id: sensor.meteonetwork_temp
        below: 30
    binary_sensor:
      - name: oggi_ha_fatto_caldo
        state: "{{ states('sensor.meteonetwork_temp')|float(0) >= 30 }}"
        device_class: heat

That should give you a sensor that goes on after 6 hours above 30°C, and off as soon as the temperature drops below 30°C. Then your automation:

- alias: Automation title here
  id: c26bb02b-c0f7-45ca-8265-ad714067b3fa
  trigger:
    - id: "am"
      platform: time
      at: "06:00:00"
    - id: "pm"
      platform: time
      at: "18:00:00"    
  condition:
    condition: or     
      conditions:
        - "{{ trigger.id == 'am'}}"
        - "{{ trigger.id == 'pm' and states('binary_sensor.oggi_ha_fatto_caldo') == 'on' }}"
  action:
    #do something

Note that if you restart HA during a hot day, the binary_sensor might not turn on as its measurement will have been interrupted.

OK thanks!

In my configuration.yaml:

binary_sensor: !include Binary_sensors.yaml

If I past your code of binary_sensor into my (and empty) Binary_sensors.yaml

template: Missing property "entities".

That’s because I’m using the “modern” configuration format, whereas you seem to be using the legacy format: in the modern format, these template sensors come under template: at the top level, not binary_sensor:. I have mine like this:

binary_sensor: !include binary_sensors.yaml
sensor: !include_dir_merge_list sensors
template: !include_dir_merge_list templates

then I’d put everything from the - trigger: line onwards into templates/binary_sensors.yaml.

Many thanks for your explanations.
I tried to go your way in configuring the configuration.yaml but, for reasons I can’t explain, I fixed the binary.sensor.yaml but got a lot of errors in the sensors.yaml file.
So until I understand what it’s doing I think it’s more convenient (and less OT for this discussion) to continue in the legacy mode.
I managed to create the working binary sensor:


- platform: template
  sensors:
    oggi_ha_fatto_caldo:
      value_template: "{{ states('sensor.meteonetwork_temp')|float(0) >= 10 }}"
      friendly_name: "Oggi ha fatto caldo?"
      device_class: heat

But there was no way to trigger it

- platform: template
  sensors:
    - trigger:
        - platform: numeric_state
          entity_id: sensor.meteonetwork_temp
          above: 30
          for: "06:00:00"
        - platform: numeric_state
          entity_id: sensor.meteonetwork_temp
          below: 30
          for: "06:00:00"
      oggi_ha_fatto_caldo:
        value_template: "{{ states('sensor.meteonetwork_temp')|float(0) >= 30 }}"
        friendly_name: "Oggi ha fatto caldo?"
        device_class: heat

Log:

Invalid config for [binary_sensor.template]: expected dictionary for dictionary value @ data[‘sensors’]. Got [OrderedDict([(‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, ‘sensor.meteonetwork_temp’), (‘above’, 30), (‘for’, ‘06:00:00’)]), OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, ‘sensor.meteonetwork_temp’), (‘below’, 30), (‘for’, ‘06:00:00’)])]), (‘oggi_ha_fatto_caldo’, OrderedDict([(‘value_template’, “{{ states(‘sensor.meteonetwork_temp’)|float(0) >= 30 }}”), (‘friendly_name’, ‘Oggi ha fatto caldo?’), (‘device_class’, ‘heat’)]))])]. (See ?, line ?).

Template Sensors defined in legacy mode don’t support triggers. Trigger-based Template Sensors are exclusively defined in the new “modern” format.

OK understood thanks