Automation trigger with multiple from: and to:

Hi all,

I’m wondering how I can make an automation where if a dropdown list goes from any of a list of states to any of a list of other states, it will trigger. It will be so many automations to do this for all combinations, I’m hoping there’s a better way… What I want to do is the below but hass won’t take it. Any ideas?

- id: heading_home_2
  alias: 'Heading Home #2'
  trigger:
    platform: state
    entity_id: input_select.sean_last_zone_dropdown
    from: 
    - "Work"
    - "Lazarska"
    - "Andel"
    to: 
    - "SmichovskeNad"
    - "WineFood"
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.sean_location_dropdown
      option: Heading Home
1 Like

If the ‘from’ states cover all the starting states you can remove them and have your trigger as:

trigger:

  • platform: state
    entity_id: input_select.sean_last_zone_dropdown
    to: ‘SmichovskeNad’
  • platform: state
    entity_id: input_select.sean_last_zone_dropdown
    to: ‘WineFood’

It would trigger on any change to the ‘to’ states regardless of the ‘from’ state. I don’t have a good answer for you if there are more than those 3 ‘from’ states the automation should consider but there is surely someone more knowledgeable here that does.

Unfortunately there are more than 10 possible starting states and I have many automations like these, so I need to define both many possible starting states and many possible ending states… I’m guessing it’s possible in a template, but I can’t figure out how to do from: and to: in a template…

I don’t think you can do it in a template trigger, but you can do it in a template condition:

- id: heading_home_2
  alias: 'Heading Home #2'
  trigger:
    platform: state
    entity_id: input_select.sean_last_zone_dropdown
  condition:
    condition: template
    value_template: >
      {{ trigger.from_state is not none and trigger.to_state is not none and
         trigger.from_state.state in ['Work', 'Lazarska', 'Andel'] and
         trigger.to_state.state in ['SmichovskeNad', 'WineFood'] }}
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.sean_location_dropdown
      option: Heading Home
4 Likes

Ooh, that’s a nice way of doing it, hadn’t even considered doing it in the condition (and didn’t know about trigger.from_state.state and trigger.to_state.state so those are big helps). Will give it a try and let you know how it worked the next time it triggers! Thanks!

You might want to check out Template Conditions if you haven’t already. Especially follow the link to the page that describes the trigger variable.

Yeah I had looked at them here and here and have even written some templates of my own already… But didn’t know how to reference from and to like you did… Where did you learn those references from?

I guess specifically from here - trigger variable for a state trigger.

Okay, I just suck. It’s in there like 8 times ;-).

lol!

BTW, the reason I test for trigger.from_state and trigger.to_state not being none is that when an entity is first entered into the state machine (i.e., first update after restart), trigger.from_state will be none. And, in theory, if the entity is ever deleted, that will also cause a state_changed event with trigger.to_state being none. If you try to use trigger.xxx_state.yyy in one of those situations, it would cause an error and the automation wouldn’t run.

Good to know, nice to stand on the shoulders of a giant on this one, I can tell you’ve done your fair share of automations ;-).

Thanks. This helped me too.