I have recently started using scripts to streamline my automations. I have a script for when I need an automation to do notifications using notify and tts:
The script uses four different fields:, media_player (what entity that should play the TTS notification), message_tts (the message that should be read out using TTS), volume_level (settings the volume) and message_notify (the message that should be sent using notify).
I call the script like this in automations:
alias: Pelletsbrännare OK (dag)!
description: Announce when pellets burner is OK
triggers:
- trigger: state
entity_id: binary_sensor.vikingbio
from: "on"
to: "off"
for: "00:00:05"
conditions:
- condition: state
entity_id: input_boolean.natt
state: "off"
actions:
- parallel:
- action: script.tts
data:
media_player: media_player.kok
message_notify: Pelletsbrännaren OK! Klockan är {{ now().strftime('%H:%M') }}
message_tts: Pelletsbrännaren OK!
- action: scene.turn_on
target:
entity_id: scene.pellets_pre_alarm
data: {}
mode: restart
initial_state: true
This works and let me for example change the model for tts.speak easily.
However! Sometimes I don’t want to use specific fields:. For example, sometimes I only want a TTS, not a text notification. If I leave out a specific field like message_notify the action is still executed but with an empty message.
Is it possible to make it so that if the field is missing in an automation, the script do not use the action?
I once looked at doing notifications this way but decided I was over complicating things for no benefit. I had to specify so many variables in my script call action that it was less work to just call the tts or notification action whenever i needed it rather than bothering with the script.
What’s the point of running a delay in parallel with all those actions…? I think you’re missing a sequence.
The easiest way is likely going to be templating the value for the actions’ enabled configuration variable based on whether the field variable has a value.
The next option would be to wrap every independent action or sequence in an If/Then action.
EDIT: I don’t know if it’s a bug or a Limited Templates thing, but I could not get the method of templating enabled to work in this situation.
I have a script blueprint somewhere that checks if a field was passed in similar to what Drew did above, and if not it sees if there is an !input, and if not passes a default.
I’d have to search for it, but that’s the way I found to work that.