Boolean conditions in automation

in the following condition example

- alias: 'Enciende Despacho'
  trigger:
    platform: state
    entity_id: sensor.mini_despacho
    to: 'on'
  condition:
    condition: or
    conditions:  <<<<<<<<<< this bit
      - condition: template
        value_template: '{{ states.sun.sun.attributes.elevation < 4 }}'
      - condition: template
        value_template: '{{ states.sensor.sensorluz_7_0.state < 10 }}'

are the conditions: a boolean AND function
ie. all must be met to proceed
I’m looking to do something like

IF sun elevation GT 10 and cloudcover GT 50%  and hotwater temp LT 40 and local time ( GT 0700 and LT 0900)

action turn on boiler  #not enough solar energy on the PV system to power elctric water 

                                  #heater.

not sure if I can nest a condition within a condition ???

thanks
Richard

1 Like

If you omit the conditon type, the list of conditions defaults to and. This is equivalent to what you are looking for with fake entity names.

  condition:
      - condition: template
        value_template: '{{ states.sun.sun.attributes.elevation > 10 }}'
      - condition: template
        value_template: '{{ states.weather.yweather.attributes.cloudcover > 50 }}'
      - condition: template
        value_template: "{{ states('sensor.hotwater') | float < 40 }}'
      - condition: time
        after: '07:00:00'
        before: '09:00:00'

In the code you have posted the conditions will be ‘or’ as you have it designated. If you don’t specify ‘or’ then by default conditions are ‘and’.

you can nest ‘or’ and ‘and’ conditions as necessary.

Thanks both , that answers it nicely
Richard

just a further question, I’ve looked through all the examples and couldn’t see an answer
for the last template suggested by petro

value_template: "{{ states('sensor.hotwater') | float < 40 }}'

I’m not sure what to use as sensor.hotwater
in my config

sensor:
  - platform: mqtt
    state_topic: "/iot4/IOT4SH01DS_A03625/temperature"
    name: "Hot Water Temp"
    unit_of_measurement: "C"

Do I use the name ? or the state_topic
sensor.< the missing bit>
the other templates were clear what was needed, but the mqtt bit I’m not sure of.

Sorry for the questions
Richard

it should be “sensor.hot_water_temp” because the entity_id of a sensor that you create is based on the name with no capitals and _ for spaces.

You can verify that by looking in your dev-states page to see what the entity_id really is.

Also, be aware that you used the wrong type of quotation mark at the end of your value_template line. It should be a double quote like th one at the beginning.

1 Like

OK thanks
Thats starting to make sense now.
richard

The congig is accepted in the automations.yaml. the states are enabled, but the hot watet aliased section are not functioning.
I’ve called the same sevice used on the TP switch “switch.turn_on”
which in the configurations.yaml uses a payload of “ON”
As the payload for the iot4relay is a “1” to turn on
do I need to define another service, or is HA intelligent enough to know the payloads for each device?

automations.yaml
  - alias: Turn On TP Link Switch
    trigger:
      platform: sun
      event: sunset
      offset: "-00:20:00"
    action:
      service: switch.turn_on
      entity_id: switch.lr_lamp

  - alias: Turn off TP Link Switch    
    trigger:
      platform: sun
      event: sunset
      offset: "+00:35:00"
    action:
      service: switch.turn_off
      entity_id: switch.lr_lamp

  - alias: 'Hot water boost on'
    trigger:
    condition:
      - condition: template
        value_template: '{{ states.sun.sun.attributes.elevation > 30 }}'
      - condition: template
        value_template: '{{ states.sensor.cloud_cover > 50 }}'
      - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float < 38 }}"
      - condition: time
        after: '07:00:00'
        before: '09:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.iot4relay
      
  - alias: 'Hot water boost off'
    trigger:
    condition:
      - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float > 46 }}"
      - condition: time
        after: '09:10:00'
    action:
      service: switch.turn_off
      entity_id: switch.iot4relay
  
  - alias: 'Evening Hot water boost on' 
    trigger:
    condition:
      - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float < 48 }}"
      - condition: time
        after: '12:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.iot4relay
  
  - alias: 'Eve hot water boost off'
    trigger:  
    condition:
      - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float > 45 }}"
      - condition: time
        after: '20:05:00'
    action:
      service: switch.turn_off
      entity_id: switch.iot4relay
      
###NOTE : the temp and time setting on the evening on and time are for testing purposes

IN HA overview all automations are shown as on
TIA
Richard

First line of the documentation:

Triggers are what starts the processing of an automation rule.

In case it’s still unclear, all your hot-water automations lack a trigger. That’s why they are ‘not functioning’.

OK I know consider me a thick as a plank, But I thought the conditions triggered

 - alias: 'Hot water boost off'
    trigger:
    condition:
      - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float > 46 }}"
      - condition: time
        after: '09:10:00'

So does that mean you can’t trigger on multiple conditions ?
If I trigger on a single event like sunrise, how long would the trigger state stay active, until the next event, or or is there a timeout ?
Or would I better in this applcation to trigger on time
sorta

- alias: 'Hot water boost off'
     trigger:
        platform: time
         at: '09:10:00'
      condition:
        - condition: template
        value_template: "{{ states('sensor.hot_water_temp') | float > 46 }}"      
      action:
         service: switch.turn_off
         entity_id: switch.iot4relay

I really need to use something like an “else” turn off after a further 30mins
otherwise its going to stay on all day

Consider a trigger as something that monitors, or ‘listens’, for a certain kind of activity or event. For example, the trigger may wait for a specific time of day or the state of an entity to change from off to on, or if an entity’s value exceeds a threshold. A trigger is not an optional part of the automation.

Effectively, a trigger is instantaneous. Using your ‘Hot water boost off’ automation as an example, the trigger detects sunrise and the automation’s execution immediately proceeds to the condition. The trigger has finished its job and won’t be activated again until the next sunrise. Meanwhile, the condition checks if the water temperature is greater than 46. If it isn’t, the automation stops immediately. If it is greater than 46, the automation proceeds to execute the action.

Consider the condition to be a filter. When the trigger occurs, the condition will check if other criteria is satisfied such as is it currently day or night, is some other entity’s state on or off, is a sensor’s value at a specific threshold, etc. A condition is optional.

Thanks
that makes it a lot clearer

Yes, I always remember:

TRIGGERS are OR by default

CONDITIONS are AND by default

1 Like