Simple automation with 3 conditions

Hi there,
I am trying to create a simple automation with 3 condition for a DHT11 sensor. One condition is OK by the way.
the condition trigger a light when it is below 23 C or above 25 using MQTT protocol.
I started testing automations but I dont find how configure OR conditional. then I decide to configure manually. Please guys you would give a light where it miss or the wrong configuration I am doing.

thanks in advance

  alias: DHT11_test
  trigger:
    - platform: mqtt
      topic: ha/temperature
  condition:
    condition: or
    conditions:
      - above: "23"
        below: "25"
        condition: numeric_state
        entity_id: sensor.temperatura
        action:
          - alias: ""
            data:
              entity_id: switch.switch2_sala
            service: switch.turn_off
      - above: "25.01"
        condition: numeric_state
        entity_id: sensor.temperatura
        action:
          - alias: ""
            data:
              entity_id: switch.switch2_sala
            service: switch.turn_on
      - below: "22.99"
        condition: numeric_state
        entity_id: sensor.temperatura
        action:
          - alias: ""
            data:
              entity_id: switch.switch2_sala,
            service: switch.turn_on```

That’s not how conditions and action work together. Triggers, conditions and actions are on the same level, i.e. you can trigger an action if all conditions (or when using an or condition, any of the conditions in your list) are true.

trigger:
  ...
condition:
  ...
action:
  ...

You may need to split your configuration into three separate automations.

Hi exxamalte,

when I split conditions I found the problem that two the condition became “true”. for example the rule over 23C sends OFF but over 25 sends ON and light flip ON and OFF each time sensor receives MQTT message.
I tested before post but I will tried again.

what I cannot figure out is how set an OR condition for my 3 rules.

thanks for your reply

The problem is that when you put above and below in a numeric_state, it means ‘between’.

You’ll need to use two separate conditions as an OR for that bit.

What I meant is: you can’t have one automation with two different actions depending on which conditions evaluate true or false.

Re-reading your automation attempt: You want a switch to turn off if the temperature is between 23 and 25 degrees, and you want that switch to turn on if the temperature is either below 23 or above 25 degrees.

Maybe, instead of conditions, you could try the template approach. Have an automation with just the trigger and an action, no conditions. The action would then look something like this (haven’t tested this, so syntax may be slightly wrong):

  action:
    - entity_id: switch.switch2_sala
      service_template: >
        {% if sensor.temperatura > 23 and sensor.temperatura < 25 %}
          switch.turn_off
        {% else %}
          switch.turn_on
        {% endif %}
1 Like

You need just the one action

Oh I just realised you had on and off. I think one for on and one for off.

have a look at this, seems close to what you’re looking for:
Uit = off, Aan = on

- alias: Vijverpomp mag uit
  id: '1511601478445'
  trigger:
    platform: numeric_state
    entity_id: sensor.rsd_temperature
    below: 6
  condition: 
    condition: template
    value_template:  >
      {{ 5 < states('sensor.rsd_temperature')|float < 7 }}
  action:
    service: notify.notify
    data_template:
      title: Vijverpomp mag uit
      message: De vijverpompen mogen uit want het is {{ states.sensor.rsd_temperature.state
        }} graden

- alias: Vijverpomp moet aan
  id: '1511601478446'
  trigger:
    platform: numeric_state
    entity_id: sensor.rsd_temperature
    above: 6
  condition: 
    - condition: template
      value_template:  >
         {{ 7 > states('sensor.rsd_temperature')|float > 5 }}
    - condition: sun
      after: sunrise
#      after_offset: '+00:00:00'
  action:
    service: notify.notify
    data_template:
      title: Vijverpomp moet aan
      message: De vijverpompen moeten aan want het is dag en {{ states.sensor.rsd_temperature.state
        }} graden.

you could do it with templates inside the action statement, or you could split into 2 automations:

- alias: automation1
  trigger:
    platform: mqtt
    topic: ha/temperature
  condition:
    condition: numeric_state
    entity_id: sensor.temperatura
    above: 23
    below: 25
  action:
    service: switch.turn_off          
    entity_id: switch.switch2_sala

- alias: automation2
  trigger:
    platform: mqtt
    topic: ha/temperature
  condition:
    condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.temperatura
        above: 24.99
      - condition: numeric_state
        entity_id: sensor.temperatura
        below: 23.01
  action:
    service: switch.turn_on          
    entity_id: switch.switch2_sala

Hi Creakyshimp,

I appreciate your response, it is accurate my situation. I tried to do with template however my IF sentences return a error, would you please give a look and enlighten me.

thanks in advance

  trigger:
    platform: mqtt
    topic: ha/temperature
  condition:
    condition: numeric_state
    entity_id: sensor.temperatura
  action:
    - service_template: >
      {% if sensor.temperatura > 23 and sensor.temperatura < 25 %}
        switch.turn_off
        - entity_id: switch.switch2_sala
        - entity_id: switch.switch1_cocina
      {% else %}
        {% if sensor.temperatura > 25 %}
          switch.turn_on
          - entity_id: switch.switch1_cocina
        {% elseif %}
        {% if sensor.temperatura < 23 %}
          switch.turn_on
          - entity_id: switch.switch2_sala
        {% endif %}
      {% endif %}

the error is depicted below

That template is all over the place and nowhere even close to how templates are used in homeassistant.

In general it is best not to try and run before you can walk.

Here is how to decide what builds a template:

First (1), you need to look at the logic of the situation. Then (2) work out what services are required for each potential outcome. Then (3) define which data is required to be passed to each service. Then (4) apply that to how homeassistant can receive the information.

(1) In your case the logic is fairly simple (assuming I’ve understood your post correctly).

  • If the temperature is 24 switch off both switches.
  • Or if it is above 25 turn one on. (elif)
  • If it is below 23 turn the other one on. (else, if it’s not 24 or above, it must be below)

(2) So your services are:

  • Turn on
  • Turn off

(3) And your data is:

  • the name of the switch. (depending on the temperature)

(4) Apply it to a homeassistant configuration:

So:

service_template: when this template resolves I want it to turn something on if it is not 24, but off if it is 24.

data_template: when this template resolves I want it to be the correct switch(es) based on the temperature.

If 24

service: homeassistant.turn_off
entity_id: switch.switch2_sala, switch.switch1_cocina

If above

service: homeassistant.turn_on
entity_id: switch.switch1_cocina

If below

service: homeassistant.turn_on
entity_id: switch.switch2_sala 

And relax.

action:
  service_template: >
    {% if states('sensor.temperatura')|int == 24%} homeassistant.turn_off 
    {% else %} homeassistant.turn_on {% endif %} 
  data_template:
    entity_id: >
      {% if states('sensor.temperatura')|int == 24 %}
        switch.switch2_sala, switch.switch1_cocina
      {% elif if states('sensor.temperatura')|int < 24 %} 
        switch.switch2_sala
      {% else %} switch.switch1_cocina {% endif %} 

The next question being, why are you using an mqtt trigger for this, rather than the state of the temperature sensor?

2 Likes

HI mf_social,

thanks for your response and explanation.
I am using MQTT triggers because my lights and sensor are setting in ESP32.
I searched ways to set them and found it no complicate it.
back to your approach, if it use states for automation, it does not need trigger and conditions, does it?

thanks again and I appreciated your responses.

The trigger would just be:

trigger:
  platform: state 
  entity_id: sensor.temperatura

With no conditions.

This way every time the reading of the sensor changes it will run the automation, and if it is under 24 the correct fan will turn on (if it is already on because the previous temperature was already under 24 then it will just stay on). If it is over 24 same principle, and at 24 switch them off.

It doesn’t matter that the sensor is set via mqtt, once it is set it has a state and once that state is different to last time is when we want to trigger the automation.

1 Like

Hi mf_social,

I tried hard with configuration for some hours without success, it seems that missing entity_ID even they are on it. The automation display this error:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘states’) for dictionary value @ data[‘action’][0][‘data_template’][‘entity_id’].

I tried with enclose entities in " or ’ or { } and [ ] but any result. I did a small change for float values, but error displayed is same with |int.

I thanked in advance your suggestions and help.

  platform: state
  entity_id: sensor.temperatura
action:
  service_template: >
    {% if 23 >= states('sensor.temperatura')|float <= 25 %} homeassistant.turn_off
    {% else %} homeassistant.turn_on {% endif %}
  data_template:
    entity_id: >
      {% if 23 >= states('sensor.temperatura')|float <= 25 %}
        switch.switch2_sala, switch.switch1_cocina
      {% elif if states('sensor.temperatura')|float < 23 %}
        switch.switch2_sala
      {% else %} switch.switch1_cocina {% endif %}

Remove if from 3rd line from bottom?