Automation trigger on a templated entity - is there a way?

Background

I use weather info in my garden irrigation package and I’d like to be able to change the weather data provider on the fly.

Question

I am well able to write everything in the package to adjust itself based on which from a variety of sensors has been selected through the package’s Lovelace UI…

…except for automations that need to be triggered on a state change of that sensor.

e.g.

    trigger:
      - platform: state
        entity_id:
          - sensor.dark_sky_daytime_high_temperature_0d

What I’d like to achieve is something like this which obviously won’t work, but is there a technique someone knows that can achieve this?

    trigger:
      - platform: state
        entity_id:
          - {{ states('input_text.weather_provider') }}

The state of input_text.weather_provider would, to match the original example above be, ‘sensor.dark_sky_daytime_high_temperature_0d’.

I’m well aware the answer is likely to be ‘no’, but it is usually worth asking!

Seems like you could just list all the weather providers under the entity_id: of the state trigger and then in a following condition you could add a template selector. Something like

trigger:
  - platform: state
    entity_id:
      - sensor.weather_1
      - sensor.weather_2
condition:
  condition: template
  value_template: " {{ trigger.entity_id == states('input_text.selected_weather') }}"
action:
  do something here

The input_text would hold the various weather providers configured which you could select from the ui.

What if I don’t know what the weather providers are ahead of time? What if I want to allow users to choose their own? This isn’t hypothetical; there are a couple of people using my package and not all have darksky which I use and have hardcoded at the moment so I’d like to make it as flexible as possible, allowing them to choose whatever provider is best for them.

So not multiple weather services but a configurable weather service. Seems like you would want to localize the weather providers to perhaps a set of templates sensors and then use the template sensors in the automations instead of using the weather provider directly. Sorry that’s all I got.

1 Like
trigger:
  platform: event
  event_type: state_changed
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.entity_id == states('input_text.weather_provider') }}

Nice. Thanks.
And I’ve (re)learnt something…
Yes, I’ve never before used,

    trigger:
      - platform: event
        event_type: state_changed

:slight_smile:

My actual use case is more complex than the general case I asked about and intuitively this seems quite inefficient although thinking about it I don’t think it is as it is probably only doing what the ‘native’ state trigger was doing.

But I’m posting my full solution just in case anyone sees a better way to do it.

    trigger:
      - platform: event
        event_type: state_changed

    condition:
      condition: or
      conditions:
        - condition: template
          value_template: >
            {{
            trigger.event.data.entity_id == 'input_boolean.irrigation_use_forecast_high_temp' or
            trigger.event.data.entity_id == 'input_number.irrigation_days_of_temp_history_used' or
            trigger.event.data.entity_id == 'input_number.irrigation_temperature_baseline' or
            trigger.event.data.entity_id == 'input_number.irrigation_actual_high_temp_0' or 
            trigger.event.data.entity_id == 'input_number.irrigation_high_temp_1' or 
            trigger.event.data.entity_id == 'input_number.irrigation_high_temp_2' or 
            trigger.event.data.entity_id == 'input_number.irrigation_high_temp_3' or 
            trigger.event.data.entity_id == 'input_number.irrigation_high_temp_4' or 
            trigger.event.data.entity_id == states('input_text.irrigation_sensor_weather_forecast_high_temp')
            }}

        - condition: state
          entity_id: input_boolean.irrigation_cycle1_adjust_for_temperature
          state: 'on'

        - condition: state
          entity_id: input_boolean.irrigation_cycle2_adjust_for_temperature
          state: 'on'

FWIW, a slight simplification:

        - condition: template
          value_template: >
            {{
              trigger.event.data.entity_id in [
                'input_boolean.irrigation_use_forecast_high_temp',
                'input_number.irrigation_days_of_temp_history_used',
                'input_number.irrigation_temperature_baseline',
                'input_number.irrigation_actual_high_temp_0',
                'input_number.irrigation_high_temp_1',
                'input_number.irrigation_high_temp_2',
                'input_number.irrigation_high_temp_3',
                'input_number.irrigation_high_temp_4',
                states('input_text.irrigation_sensor_weather_forecast_high_temp')
              ]
            }}
1 Like