Use optional input variable as a trigger entity

Hi,

I have an optional blueprint input variable illuminance_sensor and I just want to listen for state changes and use this as a trigger:

trigger:
  - platform: state
    entity_id: !input "illuminance_sensor"

This works, if the user specifies that sensor, but if the illuminance_sensor is none, I get an error. My current workaround is, to specify a dummy sensor dummy_illuminance_sensor and add it as a default: in the input variable. But is there a better way?

I also tried to use it as a template although I’m not sure what is the best way to specify that I’m not interested in a certain condition, but only in a state change for a certain (optional) entity:

trigger_variables:
  illuminance_entity: !input "illuminance_sensor"

trigger:
  - platform: template
    value_template: "{{ illuminance_entity != none and (states[illuminance_entity].state | float >= 0)  }}"

It doesn’t give any erros but it also doesn’t seem to trigger my automation.

2 Likes

Exactly my problem! I am also interested if there is a way to specify a trigger based on an optional input entity.

If the entity is not defined a state trigger reports an error and a template trigger doesn’t fire.

Did you managed to fix it?

I’m interested in this one too… did someone managed it?

This month the selector for the trigger has been added.
Perhaps you can just write the trigger as an input and then trigger on whatever you like?

Selectors - Home Assistant.

If you don’t have anything that needs triggering on, perhaps the automation is not needed?

Otherwise all triggers like that must have a valid entity.

1 Like

The trigger selector seems like the obvious choice here, I agree.
I’m just curious what other triggers would be in the blueprint as It’s not possible to run an automation without a trigger. So only adding 1 optional trigger seems weird, that’s what conditions are for.

Agreed. Thumb this up if you agree…
Trigger selector: cannot add one from selector if one already exists in the blueprint · Issue #19050 · home-assistant/frontend · GitHub.

This can be solved by putting the triggers in the input part of the blueprint and not in the trigger part.
It’s not documented anywhere, but it works.

See example below:

blueprint:
  name: default trigger example
  domain: automation
  input:

    triggers:
      name: Triggers
      description: Triggers your automation
      default:                                 #Add a default trigger list
        - platform: sun                        #
          event: sunrise                       #
          offset: "00:00:00"                   #
      selector:
        trigger:
...
mode: single

trigger: !input triggers

condition:

action:

It seems to be documented ok.

This selector does not have any other options; therefore, it only has its key.

It’s mentioned nowhere that you can use a default option.

None of the selectors individually has the fact that you can use default. The default is an option that they all have (in a blueprint). It was written that way to prevent every selector from listing the same info.
About the blueprint schema - Home Assistant.

1 Like

You’re right, I missed that.

1 Like