(I had to omit the " because otherwise I would get this error message: ""Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder: Error executing script. Unexpected error for call_service at pos 1: Template rendered invalid service: light.turn_“on”)
Yep, that was my bad. The quotes should have gone around the whole thing.
service_template: "light.turn_{{ trigger.to_state.state }}"
Templates MUST always be a string. Single line templates MUST always be wrapped in quotes. Multiline templates (the ones you see with the > or >+ etc) are not wrapped in quotes because that special character already means ‘return a string’
You can change it from the current service_template into simply:
service: light.turn_on
What I meant by that is, I prefer the way you have it right now where we are doing a state based trigger on a template switch.
The template switch is following the attribute we care about already. And, in the future, if for whatever reason the attributes changed names, or you wanted the switch to follow a different attribute, or even follow a new device (you switched to plex or something for example), the automation can remain the same because it’s following that switch state.
If you had it follow the remote.harmony_hub, if you changed any of those things as I mentioned above, you would then also have to go change the automation to do the same thing. It’s just redundant code which may have more maintenance.
Here’s how you should do the lights in a single automation.
- Create a group.
# Example configuration.yaml entry
group:
tv_light_state_group:
name: TV Lights
entities:
- switch.fernsehen
- switch.videorecorder
- switch.appletv
- switch.bluray
A group has a unique property where, if ANY single entity of the group is ‘on’, the entire group is on. Likewise, if all are OFF, the entire group is off.
We can use this as our on/off trigger. And if you add more switches to the group, it will just work without touching the automation.
Oh, and one other thing. I recommend you change the alias to something shorter without special characters. The alias for an automation turns into its name. In your example, you will have an automation.licht_an_bei_fernsehen_apple_tv_blueray_videorecorder. Though, being German, you’re probably used to long long words
Make the alias shorter and put that in the description part.
- alias: TV Lights Automatic
description: 'Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder'
trigger:
# First trigger. If anything turns to 'on', immediately trigger.
- platform: state
entity_id: group.tv_light_state_group
to: "on"
# Second trigger. If they all turn off and remain off for 1 minute, trigger.
- platform: state
entity_id: group.tv_light_state_group
to: "off"
for:
minutes: 1
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
# Change this
entity_id: light.my_light
So this one automation will now turn on the tv lights when anything in the group turns on. And, it will turn them off if the group is off for 1 minute. This means if you change states and for example, the blueray switch turns off, then a few seconds later the tv switch turns on, the lights wont go out. They only go off if ALL switches have been off for 1 whole minute. You can change that delay.