And in another episode of "What's wrong with my automation: The case of the Naughty NOT"

I thought I had this all figured out but HASS has thrown me another curve ball.

I have a seemingly simple automation that turns on a light if a camera detects motion as long as it’s NOT summer and during a certain time. But what I’m finding based on the trace for the automation is it’s actually evaluating the NOT part the opposite. I’m looking for season.season to NOT be summer but it seems to be evaluating it like I want it to be summer. And I don’t see whats wrong with the way the conditions are set up:

alias: Security - Backyard Motion Detected - Turn On/Off Light - Not Summer
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.motion_backyard
    id: backyard_motion_on_not_summer
    from: 'off'
    to: 'on'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: backyard_motion_on_not_summer
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.season
                state: summer
          - condition: time
            after: '22:00:00'
            before: '07:00:00'
        sequence:
          - service: homeassistant.turn_on
            target:
              entity_id: switch.backyard_floodlight
mode: parallel
max: 10

Trace:

Executed: November 11, 2021, 10:55:57 AM
Result:
result: false
conditions/0
Executed: November 11, 2021, 10:55:57 AM
Result:
result: true
conditions/1
Executed: November 11, 2021, 10:55:57 AM
Result:
result: true
conditions/1/conditions/0
Executed: November 11, 2021, 10:55:57 AM
Result:
result: false
conditions/1/conditions/0/entity_id/0
Executed: November 11, 2021, 10:55:57 AM
Result:
result: false
state: autumn
wanted_state: summer
conditions/2
Executed: November 11, 2021, 10:55:57 AM
Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '22:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '10:55:57.080916'
before:
  __type: <class 'datetime.time'>
  isoformat: '07:00:00'
result: false

So if you look at:

conditions/1/conditions/0/entity_id/0
Executed: November 11, 2021, 10:55:57 AM
Result:
result: false
state: autumn
wanted_state: summer

My wanted_state should be NOT summer should it not? It’s autumn so shouldn’t that condition be evaluated as true? I must be missing something obvious.

try removing the not condition and explicitly look for the states you want for the sensor. example:

- condition: state
  entity_id: sensor.season
  state:
    - spring
    - autumn
    - winter

EDIT: also the trace appears to be evaluating the season sensor condition correctly. it is looking for the sensor state to be summer and it is currently autumn so the condition evaluates as false. but the not just before it doesn’t appear to be evaluating as it should. and actually if I’m reading the trace correctly, the not condition does evaluate as true.

not → conditions/1
season → conditions/1/condition/0 and conditions/1/condition/0/entity_id/0

This State Condition will report true when the sensor’s value is summer.

              - condition: state
                entity_id: sensor.season
                state: summer

Currently, it’s autumn so the State Condition reports false.

You included a not in the State Condition:

          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.season
                state: summer

That means to report the opposite of what was determined by the State Condition. The opposite of false is true.

Then it’s working as per your requirement. The current season is autumn, which is not summer, and so the result of the condition is true.

Thanks for your replies.

Maybe the issue here then is my understanding of the trace output.

I’m going to go the suggested route of explicitly checking for each season instead of a NOT to to make things more clear and see how that works.

- condition: state
  entity_id: sensor.season
  state:
    - spring
    - autumn
    - winter

Yes.

The trace shows the results for the State Condition and for the Not Condition. You’re misreading the State Condition. It correctly indicates false (it’s currently autumn, not summer). You should be looking at the result of the Not Condition which is correctly reporting true. The upshot is that the combination of the two conditions is doing exactly what you requested: allow for every season except summer.

Replacing the State and Not conditions with a single State Condition may make it more comprehensible for you but the end-result is that it will work identically to what you already have now. It doesn’t “fix” anything, it just rearranges the logic but the final result is the same.

If it makes it clearer for you, here are the equivalent conditions implemented as Template Conditions in shorthand notation:

- "{{ not is_state('sensor.season', 'summer') }}"
- "{{ states('sensor.season') in ['spring', 'autumn', 'winter'] }}"
1 Like

Ok, so after making that change the automation is still stopping because it’s evaluating the season condition as false:

Executed: November 13, 2021, 8:44:32 PM
Result:
result: false
conditions/0
Executed: November 13, 2021, 8:44:32 PM
Result:
result: true
conditions/1
Executed: November 13, 2021, 8:44:32 PM
Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '19:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '20:44:32.244620'
before:
  __type: <class 'datetime.time'>
  isoformat: '07:00:00'
result: true
conditions/2
Executed: November 13, 2021, 8:44:32 PM
Result:
result: false
conditions/2/entity_id/0
Executed: November 13, 2021, 8:44:32 PM
Result:
result: false
state: autumn
wanted_state: autumn,winter,spring

conditions:
  - condition: trigger
    id: backyard_motion_on_not_summer
  - condition: time
    after: '19:00:00'
    before: '07:00:00'
  - condition: state
    entity_id: sensor.season
    state: autumn,winter,spring
sequence:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.backyard_floodlight

When I enter the conditions in the yaml editor as:

- condition: state
  entity_id: sensor.season
  state:
    - spring
    - autumn
    - winter

the automation editor converts to to:

  - condition: state
    entity_id: sensor.season
    state: autumn,winter,spring

so maybe that’s the problem here.

That’s just another way of providing a list of states the sensor should evaluate as true. However, it appears it is explicitly looking for the state to be “autumn,winter,spring” in which case, that will never evaluate as true. Switch back to the not condition with season sensor. Also, why are you putting a condition of type trigger and the id of this automation in the conditions section? remove your first condition and try again. Also, you can move your conditions from the choose/action section up to your actual condition section.

Try this automation

alias: Security - Backyard Motion Detected - Turn On/Off Light - Not Summer
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.motion_backyard
    from: 'off'
    to: 'on'
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.season
        state: summer
  - condition: time
    after: '20:00:00'
    before: '07:00:00'
action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.backyard_floodlight
mode: parallel
max: 10

That’s a known problem with the Automation Editor. If you compose a State Condition with a proper list in YAML, it converts it to a comma-separated string (which effectively breaks the State Condition). I believe this bug has already been reported as an Issue in the Frontend repository (on GitHub).

Just revert to using your original State and Not Conditions.