If-then-else in automation not working

Hi,

I’m trying to get an if-then-else statement working in an automation, but it’s not doing anything. I even used the automation-editor in the frontend to make sure my code is correct. It creates the same thing, but it’s not working as well. Here’s the YAML:

- alias: 'Licht Büro an/aus'
  trigger:
  - event_type: hue_event
    event_data:
      id: "hue_wall_switch_buro_button"
      "type": "initial_press"
    platform: event
  action:
    - if:
        - condition: state
          entity_id: group.group_licht_buero
          state: off
      then:
        - service: light.toggle
          entity_id:
            - light.hue_lightstrip_pc
      else:
        - service: light.toggle
          entity_id:
            - light.hue_lightstrip_pc

The light toggles are just for testing if it starts any action. Any idea what’s wrong?

depending on your event data I’m pretty sure you will need to at least remove the quotation marks from “type”.

trigger:
  - event_type: hue_event
    event_data:
      id: "hue_wall_switch_buro_button"
      type: "initial_press"
    platform: event

I have other automations with button presses, they are all working fine with quotes. For example:

- alias: 'Licht Büro aus'
  trigger:
  - event_type: hue_event
    event_data:
      id: "hue_wall_switch_buro_button"
      "type": "initial_press"
    platform: event
  condition:
    condition: state
    entity_id: group.group_licht_buero
    state: 'on'
  action:
    - service: light.turn_off
      entity_id:
        - group.group_licht_buero

I have exactly the same issue here. Currently on HA Supervised 2022.10 'ish
I both entered manually in yaml and HA’s editor as well.
Configuration check shows no parsing/syntax errors, but the created automation is not in the active automations list ?

Googled this strange issue and found no answers whatsoever.
Seems that other users apparently doesn’t have any if/then/else automation issues at all ??

If if or else isn’t occurring, then your trigger isn’t triggering. Force the event and watch for it in developer tools → events. If it shows up there, make sure your trigger matches the information in the event.

Then activate it.

Issue here is that the if/then automation doesn’t show up in the automation list at all ?
Same behaviour as when I (by mistake) create an automation with an error in a template.
The HA config check passes, as I guess HA only checks actual YAML syntax.
But automation is not in the active automation list, so I know that there’s an issue with it.

This is why I suspect I have an issue with the automation content and not the actual syntax.
I’ll have to dig further into this later after work.

post your automation…

Hi’ @petro

Sorry, my bad… :roll_eyes::man_facepalming:
Never try and play with new automation syntax late at night.
All working perfectly here now. My test automation below.

To answer the original topic question by @dantist
You need to include quotations in the state: off line → state: 'off'
I guess HA treats on/off & yes/no as a boolean (if not put in quotations to specify a string) and converts to true/false instead ??
If i copy/paste your example here and add the missing quotations, your automation is parsed and read in.
As @finity pointed out, your automation probably won’t fire with the quotations around the event data line "type"

- id: test if_then_auto
  alias: test if_then_auto
  trigger:
  - platform: mqtt
    topic: hasp/+/state/p2b89
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.event == 'down' or trigger.payload_json.event == 'release' }}"
  action:
    - if:
      - condition: template
        value_template: "{{ trigger.payload_json.event == 'down' }}"
      then:
      - service: light.toggle
        data:
          entity_id: light.kontor_loftpanel
      else:
      - service: light.toggle
        data:
          entity_id: light.office_desk_lamp

Yeah, I’ve seen that with a couple of automations in the last few days that had errors in them.

the config checker shows as valid and there are no persistent notifications saying there is an error but then after I reload/restart the automation is nowhere to be found in the automations list (even tho I created it with an id). Then looking thru the homeassistant.log file it showed that I had an error in the yaml syntax. It was right in the yaml and not even buried in a template as well.

I had no idea there was anything wrong until I noticed the automation not running and went to look at a trace and found that the automation didn’t exist in either the automations list or in dev tools states. It just didn’t exist anywhere in the system because it had been silently disabled.

it was an error in a condition (the ‘not’ syntax is wrong):

    condition:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.as_bedroom_motion
            state: 'on'
          - condition: and
            conditions:
              - condition: not
                entity_id: person.me
                state: 'home'
              - condition: not
                entity_id: person.her
                state: 'home'

here is the log from that automation:

2023-02-07 17:27:02.664 ERROR (MainThread) [homeassistant.components.automation] Automation with alias 'AS Someone is in the Spare Bedroom' could not be validated and has been disabled: extra keys not allowed @ data['condition'][0]['conditions'][1]['conditions'][0]['entity_id']. Got 'person.me'
extra keys not allowed @ data['condition'][0]['conditions'][1]['conditions'][0]['state']. Got 'home'
required key not provided @ data['condition'][0]['conditions'][1]['conditions'][0]['conditions']. Got None

there was another one that complained about the state in the trigger needing to be a string (forgot to put quotes around a number in a state trigger) that did the same thing in another automation too. So it wasn’t just a one-off thing.

so I’m not sure what’s going on with that or when it started. The config checker seems to be becoming less useful as time goes on it seems.

OT I know but I just wanted to validate that observation.

Funny thing, before I tried your solution I fired the unchanged automation, and now it’s working. Maybe one of the updates during the last two weeks fixed the issue.

Yeah, apparently the config checker is not as effective as I recall it has been previously.
Haven’t really noticed what and when, but earlier I don’t recall having automations silently dropped because of syntax errors in the actual yaml (not inside the templates)

Strange if this works :thinking: I can’t parse your automation at all, without adding the quotes (state: 'off')
Did you manually trigger the automation ? Then you skipped checking if the quotes around type is actually working. I expect it won’t