I am not a programmer of any sort – just an old guy that needs a nap. I can usually figure things out by looking at enough examples or videos but his one has me stumped.
I am trying to set a “Nap Switch” – a template switch that runs a couple of scripts.
Tuning the switch on will run scripts that:
1) turn on the Do Not Disturb on my mobile phone
2) disables my home alarm door chime (so that it does not beep every time someone opens an outside door)
Turning it off does the opposite.
I have created the scripts and when run manually they function perfectly.
Here is what I have in my configuration.yaml file for the template switch:
Currently the virtual switch functions properly (turns off and on) but it does not cause the scripts to run.
I have to admit that I do not fully understand the purpose of the value_template statement. The Home Assistant documentation says that it “Defines a template to set the state of the switch. If not defined, the switch will optimistically assume all commands are successful.” The last sentence makes it appear optional.
I’ve written a very comprehensive nap scripting system so I get where you are coming from! I’m not terribly old but who doesn’t like to nap now and then?!?
It looks like you are duplicating efforts, you really only need to have the template switch, the other input boolean really isn’t needed since you can turn and off the switch. For me I wrapped napping into a helper so I can also have states for Sleeping, Awake, One Awake (if I’m up before the wife) and so on, so this might be a consideration if you are thinking of expanding it. Why the helper? Because now all my “noisy” things like voice prompts or computer sounds only play when “Sleep Mode is Awake”, which is easier than having multiple rules for multiple states of sleep.
Try running your scripts using the script turn on service like this:
A template switch is essentially an input boolean and script/automation combined, so the boolean helper isn’t really necessary. Personally, I would move the script calls to an automation instead of using a template switch.
If you just really want to keep the template switch, you could set your template to be based off the dnd state of the phone or the state of the door chime. Something like (make sure to use your actual door chime entity):
value_template: |-
{{ is_state('sensor.pixel_31_xl_do_not_disturb_sensor', 'priority_only')
or is_state('input_boolean.allow_door_chime', 'off') }}
The value_template defines the state of the switch entity. If the template renders “true” the switch’s state will be shown as “on”, if it renders “false” the switch’s state will be shown as “off”.