Correct usage of boolean entity state in Automation

Hi everyone,

I am getting a strange behaviour when trying to set up an automation using a choose conditions for the resulting actions.
I want to check if the sun goes down and also is below an elevation of 2.
In that case I want to modify a switch to on state, otherwise it should turn off.

The conditions and trigger all match so that for the sake of simplicty I am removing them.
The statement that acts strange is the check for the boolean state attribute “rising” of “sun.sun”.

alias:  Turn my switch on/off
description: ''
trigger: []
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            below: '2'
          - condition: state
            entity_id: sun.sun
            state: 'false'
            attribute: rising
        sequence:
          - type: turn_on
            device_id: 1234567890
            entity_id: switch.my_switch
            domain: switch
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: 'true'
            attribute: rising
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            above: '-4'
        sequence:
          - type: turn_on
            device_id: 1234567890
            entity_id: switch.my_switch
            domain: switch
    default:
      - type: turn_off
        device_id: 1234567890
        entity_id: switch.my_switch
        domain: switch
mode: single

It does not matter what I try the result is always that the switch is off even if the conditions are matching.
I tried to write the state in different formats (“true”, “True”, “1”) but none seems to be working.
If I change the condition types for “state” to template using the following pattern it works like a charm:

alias:  Turn my switch on/off
description: ''
trigger: []
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            below: '2'
          - condition: template
            value_template: '{{state_attr("sun.sun", "rising") == false}}'
        sequence:
          - type: turn_on
            device_id: 1234567890
            entity_id: switch.my_switch
            domain: switch
      - conditions:
          - condition: template
            value_template: '{{state_attr("sun.sun", "rising") == true}}'
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            above: '-4'
        sequence:
          - type: turn_on
            device_id: 1234567890
            entity_id: switch.my_switch
            domain: switch
    default:
      - type: turn_off
        device_id: 1234567890
        entity_id: switch.my_switch
        domain: switch
mode: single

Any idea what I am missing or doing wrong when using the state?

I’ve been away for a while, and haven’t tried using the new attribute option for state conditions, but I’d guess that since the attribute is a bool and not a str, you need to specify the desired state also as a bool and not a str. I.e.,:

          - condition: state
            entity_id: sun.sun
            state: false
            attribute: rising

I.e., lose the quotes.

I’m struggling with this true and it appears as though within Configuration>Automations when you type in true it changes it to ‘true’ automatically. Even if you do it in YAML. Maybe this is a bug or maybe they should allow you to input true/false Boolean options, I’m not sure. I may have to learn templating.

It’s one of the Automation Editor’s several deficiencies. Another one is that it converts a YAML list into a comma-separated string.

If you want to enter a boolean value and not have it converted into a string, you will have to separate the automations you create manually from those created (or modified) using the Automation Editor. You have full control over the automations you create with a text editor (like VS Code) because, unlike the Automation Editor, it won’t automatically apply its own set of formatting rules to your code.

Not sure if this will work for everyone but its currently working for me. Instead of saying false I used a 0 and it works fine

Any news, about this? I do not believe that there is such big oversight from there side. Anyone knows if there is a defect about this created already?

I spend more than 3hours trying to understand what is going on, and why my automation is not working.
At the very least, add a check in the UI if the text is “true” or “false” than display some tooltip/

1 Like