found character '%' that cannot start any token in "/home/hass/.homeassistant/automations/office_brightness.yaml", line 8, column 6
That location equates to the first % just before the if. The above template code does parse fine in the dev template tester.
The below code works in my config as far as not throwing the parsing error, but it doesn’t actually work. Note that the only change is to remove the "- " from the beginning of “service_template”.
I’ve never seen anything in the documentation to support this, but I’ve never been able to run a service_template and a data_template successfully in the same automation.
I would implement this as 2 automations. One for the zero state, and a different one for other states.
Try changing is_state('trigger.to_state', '0') to is_state('trigger.to_state', '0.0') in the version that doesn’t do anything but also doesn’t error. Other than that, I can’t see anything wrong.
I actually did try that at one point, but no change. Even if that were the case though (that it was comparing to the wrong value), then it should still call light.turn_on as that’s the else clause, but even that didn’t work… the slider didn’t control the brightness of the light. In the code at the very top, it does.
This kind works for me. The light turns on and sets brightness based on the slider. However the turn off service does not allow the brightness key and therefor wont turn off the light.
You would need to add another template to not include the brightness key if you are turning off the light.
Mainly… because I had no idea that it was a thing, but I will be looking at it for other things.
I ended up using AppDaemon, though. It was fun. I’m sure there’s some optimizations I can make, but I wanted to learn AppDaemon anyway, and this seemed like a good place to start. I love that I can live edit automations.
import appdaemon.appapi as appapi
#
# BrightnessSlider App
#
# Args:
# light: The full entity name of the light to link with the slider
# slider: The full entity name of the slider to link with the light
#
class BrightnessSlider(appapi.AppDaemon):
def initialize(self):
self.handle_slider = self.listen_state(self.update_brightness, self.args["slider"])
self.handle_external = self.listen_state(self.update_slider, self.args["light"], attribute='brightness')
def update_brightness(self, entity, attribute, old, new, kwargs):
old_value = int(float(old or 0))
new_value = int(float(new or 0))
if new_value != old_value:
# TODO: Set a new device named "appdaemon.brightness_slider_<light>" to the brightness value, and maybe one ending in "_pct" to the percentage value
if new_value == 0:
self.turn_off(self.args["light"])
self.log("Turning {} off".format(self.args["light"]))
else:
self.turn_on(self.args["light"], brightness = new_value)
self.log("Setting {} brightness to {}".format(self.args["light"], new_value))
def update_slider(self, entity, attribute, old, new, kwargs):
old_value = int(float(old or 0))
new_value = int(float(new or 0))
if new_value != old_value:
status = self.set_state(self.args["slider"], state = new_value)
self.log("Setting {} to {}".format(self.args["slider"], new_value))