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
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.
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.
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.
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.