I am attempting to write a script that will check conditions and aborts if they are not met but I realized that if the script aborts then the trigger condition will not get reset and then can not be re-triggered to run the script again.
If the timeout fails (I am planning on adding other conditions that will need to be met before it is OK to shutdown) I would like to reset the input_boolian back to true so telling Alexa to “turn off the printer” will run the script again. Does anyone have any suggestions?
Do I create another input_Boolean to track the shutdown status and another automation to reset the first input_boolian to true if the script.print_shutdown is off without clearing the shutdown status? Is there a cleaner method?
I have also tried setting the trigger to the following but the script did not fire with a off to off request.
trigger:
platform: state
entity_id: input_boolean.3d_printer
# from: 'on'
to: 'off'
Instead of using an input_boolean, you can use a switch template. The switch template turn off service can execute the script. The turn on sequence can possibly turn it on? or do nothing. But the value-template can just monitor the state of the printer. So the state will always reflect whether the printer is on or off. There will be no need to reset any input_boolean. Also, going this route allows you to get rid of your automations.
switch:
- platform: template
switches:
printer:
value_template: "{{ is_state('sensor.print_current_state', 'Operational') }}"
turn_on:
# YOU WILL NEED A SERVICE HERE. IT IS REQUIRED.
turn_off:
service: script.print_shutdown
I will need to looking to the template switch but I am not sure it will work.
The goal is the following:
Ask Alexa to turn on the printer -> automation to turn on the outlet from the input_boolian
Ask Alexa to turn off the printer -> check to make sure the printer is not busy, shutdown print server, delay, and then turn off the outlet.
After thinking about what the end goal is, I think I need to remove the timeout as I want the print to finish but for other cases I would like to be able to re-trigger a command that may have failed.
Using the above example, should I change the value_template to be the status of the outlet as sensor.print_current_state should only be used to make sure it is not Printing before shutting down.
I use template switches for alexa instead of input booleans because you can’t turn an input boolean on if it’s already on. Same if it’s off, you can’t turn it off if it’s off. A switch does not have those limitations so you don’t need to reset it. Just my 2 cents. I think template switch is the way to go.
template switch
Pros:
Can call turn_on / turn_off regardless of the state of the switch.
Entire script can be placed into turn_on / turn_off section of the switch. No need for on automation or off automation.
Is in the switch domain. Alexa will treat it like a hardware switch.
cons:
None
input_boolean
pros:
can store a state.
cons:
have to maintain a stored state with multiple automations.
have to maintain automations that react to the state changes.
here’s an example complex template switch:
# AUDIO 2, USED FOR ECHO AND MUSIC ZONE 2
audio_2:
value_template: "{{ is_state_attr('media_player.yamaha_receiver_zone_2', 'source', 'Echo') and is_state('switch.floating_outlet_switch', 'on') }}"
turn_on:
- service: switch.turn_on
entity_id: switch.floating_outlet_switch
- service: media_player.turn_on
entity_id: media_player.yamaha_receiver_zone_2
- service: media_player.select_source
data:
entity_id: media_player.yamaha_receiver_zone_2
source: Echo
- service: media_player.volume_set
data:
entity_id: media_player.yamaha_receiver_zone_2
volume_level: 0.7
turn_off:
- service: switch.turn_off
entity_id: switch.floating_outlet_switch
- service: media_player.turn_off
entity_id: media_player.yamaha_receiver_zone_2
The switch will only show that it’s on if the yamaha source is set to Echo and the outlet is turned on.
The turn on service turns on the outlet, turns on the media player, selects the source for the media player and then sets the volume
the turn off service turns off the switch and the receiver.