When the input is a entity, use that as a trigger, when empty, do nothing

I have simple YAML code (it`s part of larger blueprint):

blueprint:
  name: Trigger for any entity
  description: Allows the user to select any entity, which will then be used as a trigger.
  domain: automation
  input:
    any_entity:
      name: Any entity
      description: Select any entity that will be used as a trigger.
      selector:
        entity: {}  

trigger:
  platform: state
  entity_id: !input 'any_entity'  
  to: 'on'  

I would like to adapt this code to two situations:

  • When the user selects an entity, it will be used as a trigger (like now)
  • When the user leaves the field empty, then the entity declaration will be omitted.

That input cannot be optional because you cannot have an empty trigger.
Because limited templates are all you get in triggers, you likely cannot template around that being empty. It might be possible to set a default, but you may need to chat up a template expert to help you there.

It would be good to understand why you want to have that entity ID optional.

Do you know you can also blueprint scripts? You can also pass variables to scripts and it’s much easier to make that optional using conditions.

An automation without a trigger is a script. So, make a blueprint script which you can reuse and/or use as an “automation without a trigger”. Use said script then also in your automation or make a blueprint if you’re going to have several of these automations.

My blueprint is larger and has several settable inputs and are used as triggers. But one of them I want to make as option and must be used as a trigger when it’s not empty. I changed example code for better undestand my problem:

blueprint:
  name: Trigger for any entity and optional entity
  description: Allows the user to select any entity and optional entity if he want.
  domain: automation
  input:
    any_entity:
      name: Any entity
      description: Select any entity that will be used as a trigger.
      selector:
        entity: {}  
    optional_entity:
      name: Optional entity
      description: Select this when want to use as trigger
      default: ''
      selector:
        entity: {}  

trigger:
  platform: state
  entity_id: !input 'any_entity'  
  to: 'on' 

  platform: state
  entity_id: !input 'optional_entity'  #how to change this to use this as trigger when 'optional_entity' are not empty
  to: 'on' 

Working with pseudo code is not helping us understand. If you want help you should point us to a code share site or a gist where we can see all of it.
At this point my first answer still stands. A state trigger must have an entity. You need to generate something within limited templates constraints if a value may or may not be there to trigger on.
Or you require an entity always with that default being something that will never trigger.

Yeah, we don’t need hypotheticals. Give real use cases. I have a strong suspicion you are completely over generalising the problem (maybe). The problem is we don’t know your problem — only what you think the solution might be.

I want make simple blueprint for controling lights with motion sensors and switches. But some of my lamp don`t have any switches and I want control whem only with motion sensors. I want to make this blueprint universal for situation when switches must be used and when they won’t.
I am currently writing this blueprint and thought it would be easy to do.

blueprint:
  name: Lights
  domain: automation
  input:
          
    controlled_entities:
      name: Controlled Entities
      selector:
        entity:
          domain: switch
          multiple: true
          
    switch_entities:
      name: Light Switch
      selector:
        entity:
          domain: switch
      default: ""
        
    motion_sensors:
      name: Motion sensors
      selector:
        entity:
          domain: binary_sensor
          multiple: true
          
    delay_time:
      name: Delay time after moment when all motions sensors goes off
      default: 0
      selector:
        number:
          min: 0
          max: 360
          unit_of_measurement: sekundy
          mode: slider
         
    
variables:
  controlled_entities: !input switch_entities
  switch_entities: !input switch_entities
  motion_sensors: !input motion_sensors
  delay_time: !input delay_time
 
trigger:
  - platform: state
    id: 'motion_on'
    entity_id: !input 'motion_sensors'
    from: 'off'
    to: 'on'
    
  - platform: state
    id: 'motion_off'
    entity_id: !input 'motion_sensors'
    from: 'on'
    to: 'off'

#in here I want to declare trigger for switch when it`s no empty

action:
# in here will be a logic

1 Like

That’s a bit more helpful. One more question.

Probably a language thing, but if a light doesn’t have a switch, how is the motion sensor going to control it. How is anything going to control it?

Motion sensors are Zigbee sensors.

Switch entities can be a Zigbee Buttons or inputs (Wall Switch) from Smart Relays (example Shelly 2.5 Plus)

Controlled entities can be:

  • smart bulb,
  • normal bulbs connected to the Smart Relay outputs,
  • LED strips (Yeelight Light Strip)
  • TV backlight (Philips Ambilight)

In the configuration without using switches, I only want the Zigbee motion sensors to switch on the LED strips or TV backlight when someone walks around the room.

Ok, so nothing is empty. You have motion sensors as triggers, and inputs for the lights to turn on when triggered.

You might want to set a scene and have the motion sensors trigger scenes.

You can use a template trigger. By omitting or setting the specific input to None, you can check for that or its existence in this template trigger, and if defined do your other checks (say for a specific state) in that same template.

{{ switch_entities is defined and ... }}

I don`t understand - If I have two scenarios:

  1. The bulb that I want to control with the Zigbee button and Zigbee motion sensors,
  2. The bulb that I want to control with Zigbee motion sensors,
    In the second scenario “switch_entities” I have empty

Something like this?

  trigger:
    - platform: template
      value_template: '{{ !input SENSOR != none }}'

No, use your variables you had previously. You need that intermediate step. You can’t use the input directly like that.

Until you understand how to combine automations, you have 2 automations (or Blueprints). Get one working for motion lights. Get another working with the switches. Then you can work on using trigger ID’s and choose statements and do your selecting what happens if you insist on complicating your life by combining things.

Unless is direct interaction and logic needed for the 2 scenarios to interact, they do not need to be in the same automation.

1 Like

Ok. So to sum up, there is no other way to do it than by creating two blueprints?

Automations (or scripts) are BP’s. What I’m saying is get it working as those first, then add the complication of inputs and variables. Tackle 1 problem at once. If you are combining, as I said above, trigger ID’s and Choosse Statements are a good way to go. There are others like if statements and more I haven’t thought of.
Generally you will find that a general trigger is better then a specific one sort out what triggered after because after the trigger, full template rules are in effect.

You may find in this case that the BP doesn’t actually buy you anything.

1 Like

Ok, I added defualt value with not existing “input_button” switch in blueprint and it`works.

Here is example:

blueprint:
  name: Lights
  domain: automation
  input:
          
    controlled_entities:
      name: Controlled Entities
      selector:
        entity:
          domain: switch
          multiple: true
          
    switch_entitiy:
      name: Light Switch
      selector:
        entity:
          domain: switch
      default: "input_button.WITHOUT_SWITCH"
        
    motion_sensors:
      name: Motion sensors
      selector:
        entity:
          domain: binary_sensor
          multiple: true
          
    delay_time:
      name: Delay time after moment when all motions sensors goes off
      default: 0
      selector:
        number:
          min: 0
          max: 360
          unit_of_measurement: sekundy
          mode: slider
         
    
variables:
  controlled_entities: !input switch_entities
  switch_entity: !input switch_entity
  motion_sensors: !input motion_sensors
  delay_time: !input delay_time
 
trigger:
  - platform: state
    id: 'motion_on'
    entity_id: !input 'motion_sensors'
    from: 'off'
    to: 'on'
    
  - platform: state
    id: 'motion_off'
    entity_id: !input 'motion_sensors'
    from: 'on'
    to: 'off'

  - platform: state
    id: 'switch_off'
    entity_id: !input 'switch_entity'
    from: 'on'
    to: 'off'

action:
# in here will be a logic

One more thing. Where is a way to set default value when multiple: is set to true in input: section?

same as

you did here

When I use a default value in which the name doesn’t contain a domain part, I receive the following error:

Message malformed: Entity 0 is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

This is why I included ‘input_button.WITHOUT_SWITCH’ in the default name.

As expected.
Sometimes the rules apply if you want then to or not.