0.46 Need help updating automation fix deprecation of 'state' in trigger

So, with the release of 0.46, I am getting these fun warnings :slight_smile:
WARNING (MainThread) [homeassistant.components.automation.state] ‘state’ is deprecated. Please rename ‘state’ to ‘to’ in your configuration file.

I have multiple automations using this, and frankly, neither the warning message or the component documentation are any help here. Changing from ‘state’ to ‘to’ makes no sense.

# Turns on lights at sunset
- alias: 'Candle Lights on'
  trigger:
    # Prefix the first line of each trigger configuration
    # with a '-' to enter multiple
  - platform: sun
    event: sunset
    offset: '-00:10:00'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.holiday_mode
        state: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: group.candle_lights

Find all TRIGGER CONDITIONS that use “state:”

Change those to “to:”

Note: Only TRIGGER conditions! Your example is not one giving a warning

2 Likes

I don’t understand this at all. The documentation from the release notes leads to a 404, the above explanation isn’t clear to me, and my logs are unhelpful. I’m not sure which policy is failing on my system.

Any additional help is appreciated.

I think I might have tracked down a problematic automation. Any thoughts?

- alias: "060_0005 Motion On (Night) - Kitchen Cabinets"
  trigger:
    platform: state
    entity_id: binary_sensor.kitchen_motion_sensor_motion
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.kitchen_light
        state: 'off'
      - condition: state
        entity_id: switch.kitchen_cabinet_lights
        state: 'off'
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: switch.kitchen_cabinet_lights

Nope, this one is fine. What you are looking form is an automation that has a trigger that looks like this:

trigger:
  platform : state
  entity_id : this_is_my_entity
  state:  'on'

The change is documented here under state trigger:

So if I am just looking for trigger’s using ‘state’, do I change the line reading
state: 'above_horizon'
to
to: 'above_horizon'

???

# Turns on Christmas Tree lights
- alias: 'Christmas Tree on'
  trigger:
  - platform: state
    entity_id: sun.sun
    state: 'above_horizon'
  - platform: state
    entity_id: group.tracked
    from: 'not_home'
    to: 'home'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.tracked
        state: 'home'
      - condition: state
        entity_id: input_boolean.holiday_mode
        state: 'on'
      - condition: state
        entity_id: group.tree
        state: 'off'
      - condition: time
        before: '22:30'
  action:
    service: homeassistant.turn_on
    entity_id: group.tree

You got it; just like that “to:” in the trigger. Every one you fix should reduce the warning count by one at startup.

Yes; that will fix it. You need to do that for all automations.

Thanks!

The key was looking just at the trigger portion of the automation. I had the same problem with the use of ‘after:’ in some triggers and was also able to track those down.

@amelchio gave me this link in the comments to the issue that prompted the PR and it may help you to understand it better; I know it helped explain it to me:

I think the reason that I liked the previous method better, was that I don’t want to list all possible ‘from’ states in the trigger, and it just doesn’t look right only listing a ‘to’ state.

So while this works, it is a little odd.

- alias: 'Christmas Tree on'
  trigger:
  - platform: state
    entity_id: sun.sun
    to: 'above_horizon'

Why not? It’s the only state you’re interested in after all. Kinda makes sense to me.

To me this also makes sense…

If I want to trigger on a specific state change I can use "to and “from”.
If I want to trigger on a state change ending state I can use “to”
If I want to trigger on a state change starting state I can use “from”

It allows me to do everything I need to do and in a logical way.

I typically am explicit in my triggers and use to and from; unless more general behavior is needed.

2 Likes

I don’t understand much

is this wrong? how to9 fix?

  - alias: "Motion Entrance - OFF strip lights"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_sensor
        state: 'off'
        for: '00:00:15'

Change state: 'off' into to: 'off'

only
to: ‘off’

is same as

from: ‘on’
to: ‘off’

?

Those are identical if you only have two possible states (like on/off) because any change to a state must be coming from the other.

If you have more than two states (like playing/paused/idle), omitting from will make the lone to match any change to that state, no matter where the state is coming from.

3 Likes