Conditions "or"/"and" or templating: What works better?

Hi there,

after getting some automations running with templates (to limit the timeframe in which the automation is triggered) I thought templates were quite “cool” to reduce the lines of code and improve my automations… Some automations seem not to take into acccount all conditions and looking at the automations in the HA webinterface, it says conditions like “or” or “and” are note supported, e.g. for something like this:

- id: something
  trigger:
  - ...
  condition:
    condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        ...
      - condition: state
        ...
    - condition: sun
      ...
    - condition: sun
      ...
  action:
  - ...

This automation is also triggered if only one of the conditions in the “and”-block is true. Therefore I tried something similar with templates, but ran into a problem as well (different use case, therefore with “or”):

  - condition: template
    value_template: "{{ is_state("device_tracker.someone", "home") or is_state("device_tracker.someone_else", "home") }}"

Checking the config, an error occcurs

expected <block end>, but found '<scalar>'

at the beginning of the first “device_tracker…”. The template expression itself renders correctly as true or false in the template editor in the webinterface. Would I have to use a different type than “value_template” (I tried data_template as well but got the same error.)?

I can’t speak to the automation editor not supporting mixed and/or conditions since I don’t use it.

But the issue with your value_template is that you have nested quotes of the same style so it confuses the renderer. You need to do it like this:

- condition: template
    value_template: "{{ is_state('device_tracker.someone', 'home') or is_state('device_tracker.someone_else', 'home') }}"

The internal ones need to be opposite the external ones.

1 Like

Thank you very much, @finity! That helped a lot, now I’m a big fan of templates again. :wink:

Does anyone know whether “or” in conditions is still supported? If not, I would open an issue to correct the Docs accordingly.

It should be. I haven’t seen anything that said otherwise.

Why are you asking? Something not working?

Or definitely works for me…

Strange…

In the webinterface, HA tells me when checking automations online:
Unsupported condition: or

And some automations, e.g. the structure below for some lights, were executed even if only one of the conditions in the “and-block” was true… So I guess I’m doing something wrong. :joy:

- id: something
  trigger:
  - ... (will be triggered if no presence on one of the presence detectors for 30 minutes)
  condition:
    condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        ... (presence detection 1 'not_home')
      - condition: state
        ... (presence detection 2 'not_home' and so on)
    - condition: sun
      ... (e.g. after sun down)
    - condition: sun
      ... (e.g. after sun up)
  action:
  - ...

Or isn’t supported with the automations GUI editor but they still work.

Regarding your specific automation, you’re not really providing enough of the ‘real’ automation to help you troubleshoot. Don’t post generic ‘examples’ post a real automation as that will enable you to get help.

Ah, ok… That confused me then and solves part of my problem. :smile:

Sorry, I did not want to overload the post with “unnecessary” lines, but am happy to share them:

- id: turn_off_light
  trigger:
  - entity_id: device_tracker.phone_1
    platform: state
    for:
      minutes: 30
    to: not_home
  - entity_id: device_tracker.phone_2
    platform: state
    for:
      minutes: 30
    to: not_home
  - platform: sun
    event: sunrise
    offset: "+01:00:00"
  condition:
    condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        entity_id: device_tracker.phone_1
        state: 'not_home'
      - condition: state
        entity_id: device_tracker.phone_2
        state: 'not_home'
    - condition: sun
      after: sunrise
    - condition: sun
      before: sunset
  action:
  - service: switch.turn_off
    entity_id: switch.light

This is executed even if one of the devices is home if the other one was gone for 30 minutes (sun is not up, so it’s definitely the presence detection). Looking at the device history shows the phone which is home as “home” without any interruptions, and also in the logbook it is no entry of the second phone being “not_home” at any of the times in question.

couple of things… not sure but in your triggers you don’t have not_home in quotes but in the conditions you do - may not make any difference.

But… in your trigger you have 1 hour after sunset… but your other triggers could be executed (remember triggers are OR - any trigger being met will trigger the automation). So if one of you is not home for 30 minutes that is a trigger. However it then evaluates your conditions and both of you need to be not home… but your 1hr after sunset isn’t a condition so the action could be triggered at any time after sunset…

So you have 3 conditions ANY of which have to be true and one of those has 2 conditions nested, both of which must be true.

(And it’s damn hard replying on my iPad lol when I can’t see the bloody conditions!)

This was my original reply but while typing it I may have unterstood your point. :smiley:

I'm not completely sure I got your point, so please forgive me if I misunderstood:

Did you mean sunrise instead of sunset?
If so, that is actually the idea:
- both leaving home should turn of the lights
- sunrise +1 hour should turn of the lights no matter what

The problem is, it turns off the lights after one is gone for 30 minutes, ...

And that’s where I think I got it. :wink: 30 minutes after one of the phones leaves it is still before sunset, so one of the or conditions is fulfilled and the automation is executed.
When I get back home tonight I’ll have to check my backup (which I hopefully made) to see if this is really the way I implemented it (I had to reconstruct some parts as I switched to using templates as mentioned above)…

1 Like

Hahaha… it’s so hard to see the wood for the trees sometimes! I guess just understand it works correctly IF your logic is correct. If it’s not working it’s because of an error on your part. If you state what you want to happen then it’s easy to see the error (when you also post the automation)

1 Like