If Then automation not triggering (peak / off peak energy hours)

I created three helpers for time of day to my peak energy hours from my electric company. I then added in a seasonal condition for my automation.

I created two input booleans that will get turned on if the conditions are met to indicate I am currently in peak hours for energy billing. I think my issue is that the If Then section of the my automation is treating these as AND statements and not OR statements. Basically if any of my two time of day sensors goes off, I want HA to check if the Season value matches “autumn”, “winter”, OR “spring” AND if one of my binary sensors is also “on”.

Here’s the YAML (note, I have the trigger set to “any state”; its shows as null in the YAML view though)

alias: Peak Winter Hours
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.smeco_winter_peak_b_hours
    from: null
  - trigger: state
    entity_id:
      - binary_sensor.smeco_winter_peak_a_hours
    to: null
conditions: []
actions:
  - if:
      - condition: state
        entity_id: sensor.season
        state: autumn
      - condition: state
        entity_id: sensor.season
        state: winter
      - condition: state
        entity_id: sensor.season
        state: spring
      - condition: state
        entity_id: binary_sensor.smeco_winter_peak_a_hours
        state: "on"
      - condition: state
        entity_id: binary_sensor.smeco_winter_peak_b_hours
        state: "on"
    then:
      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.smeco_peak_winter_active
        data: {}
    else:
      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.smeco_peak_winter_active
        data: {}
mode: single

Any thoughts?

Hi Stan,

I would ask if the triggers you have set-up actually ever report as null. Null can happen, but seldom does…

Look at the entities in the states page and see what states they actually are reporting.

Open your Home Assistant instance and show your state developer tools.

Thank you for the reply! While I was looking into what you said, it hit me that I need to use the OR condition prior to using If Then.

Just in case someone else benefits from it, here’s the YAML that appears to be working now:

alias: SMECO Peak Winter Hours
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.smeco_winter_peak_b_hours
    from: null
  - trigger: state
    entity_id:
      - binary_sensor.smeco_winter_peak_a_hours
    to: null
conditions:
  - condition: state
    entity_id: sensor.season
    state: autumn
  - condition: state
    entity_id: sensor.season
    state: winter
  - condition: state
    entity_id: sensor.season
    state: spring
actions:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.smeco_winter_peak_a_hours
            state: "on"
          - condition: state
            entity_id: binary_sensor.smeco_winter_peak_b_hours
            state: "on"
    then:
      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.smeco_peak_winter_active
        data: {}
    else:
      - if:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.smeco_winter_peak_a_hours
                state: "off"
              - condition: state
                entity_id: binary_sensor.smeco_winter_peak_b_hours
                state: "off"
        then:
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.smeco_peak_winter_active
            data: {}
mode: single

It still sounds weird that the state would be ‘null’ :thinking:

According the docs, it can be

A binary sensor can have two states: on or off .
In addition, the entity can have the following states:

  • Unavailable: The entity is currently unavailable.
  • Unknown: The state is not yet known.

In some cases, it can also be something else (doors, heating), but under the hood (in automations) it would still be on/off.

@aceindy — putting to: null in the trigger means “trigger off any state change, but not attribute changes”. See here:

@avipullyoursocksup — there’s no way your YAML above will work like that. The conditions: block is AND by default, but your season sensor cannot be in three states at once. You might find this recent post of mine helpful:

1 Like

So that would be the same as not putting any state… :thinking:

as most binary_sensors don’t have attributes :smiley:

For the case of a sensor with no attributes, yes. Binary sensors created with the Ping integration, for example, do have attributes that change frequently. Might be safer to add it than leave it out.