…it will fail every time light.turn_off is used. How do you get around this? I tried to use jinja to remove the brightness part but I didn’t manage. Is that possible. Any help would be appreciated.
This is why I don’t use service_templates. They just don’t work if you try to set any attribute. What we really need to do is get a PR going with an update that doesn’t push attributes if the attributes don’t exist in the device
the problem with that is jinja can only output one item at a time and it cannot output complex objects like dictionaries, lists, etc. You may not realize it, but what you are trying to do is essentially output a dictionary. I’ve spent hours trying to get service templates to work with turn_on / turn_off. Your best option is to have a series of scripts that you execute or move to appdeamon.
I definitely don’t. You might very well be right. I don’t really know what the limitations are. I can’t see the limitations you are talking about on the HA page on templating. How did you come to that conclusion?
I’ve tried everything I can think of to output a value_template as a json object. Whatever built-in HA uses just converts complex objects to string representations of said object.
If that is true, it seems to me to be a pretty serious shortcoming. I imagine that the scenario where you want to press a button to decrease a light incrementally until it turns itself off, is pretty common. It shouldn’t be this hard to implement.
That (i.e., light.turn_on with a brightness of 0) only works for some lights. It depends on the light platform. E.g., it can work for a Z-Wave dimmer, but I’ve been in discussions with people trying to do this where it didn’t work for their particular kind of light.
OK, I’ve solved it now. This is such basic functionality, so I’ve made it as simple to follow as possible for those who happen to come across this topic in the future. This is how I did it:
1: Enable Python scripts in the config file with: python_script: and create a python_scripts folder in your HA folder
2: put the following script in the python_scripts folder, call it brightness_adjust.py
I have attempted to implement this for use with a trigger based on a MQTT level message, but the script is returning a DICT error, as I am failing to correctly pass the entity_id to the service template
First, you aren’t passing brightness to the script. You are passing brightness_pct. The script isn’t looking for a brightness_pct variable, it’s looking for the brightness variable. Remember, this is not a light, it’s a script. It does not have all the helpers and functionality of a light, I.E. it doesn’t understand the concept of brightness_pct or brightness for that matter, we are just passing variables to the script.
For example, the data_template in the script could have been:
Second, you have a spelling error on service_template. You spelled it: service_tempalte.
So to fix those issues, you need to use brightness as a variable, the number needs to be 0 to 255, not 0 to 100. And you need to rectify the spelling error.
You got back much faster than i expected; I had spotted the logic and spelling issues after posting, and with a little more tweaking my final working sample looks as follows: