Hello everyone, I’m trying to create a generic script that allows me to manage my outdoor lights which can be activated or not with a parameter (Automate Gazebo #1 and/or #2 for example). This generic procedure will receive optional information concerning the desired state of the light (Brightness, Color Temp, Color Name…). A problem occurs when I want to build the data block that will be sent to light.turn_on. I’ve tried different ways of sending the information, but I still can’t do it.
I think the dictionary is incorrect, I believe it should be more like this:
{'brightness_pct': 60, 'color_name': 'red'}
What you are passing would be treated like a list in the data, which works for some things (like sending multiple entity ID’s for example) but I don’t think it would work for a single entity command.
Although, looking at your code, you are defining the brightness and color code, so testing if they are set isn’t doing anything at all since there is never a time they are not set. That’s minor and doesn’t cause problems really, just an observation.
The piece of code is minimal and only for testing the solution. The problem is that we can’t set var_color_temp at the same time as var_color_name, even if one of the two is null.
There’s a lot of jinja and that tends to get hard to read if you have to use this more than once. Consider putting this template in a custom_template / macro so you can call it. (Assuming you are going to use it more than one place).
The difference to remember is the return will be a string, and you may need to cast it to whatever if a string is not what you need returned.
Can you explain why you have chosen to create a template that tests if each individual Jinja2 variable exists when all three variables do in fact exist and are hard-coded into the template itself?
Are the three Jinja2 variables actually script variables passed to the “generic” script that manages your outdoor lights?
Simply because I don’t want a default value. If the procedure is asked to adjust color, I don’t want to touch Brightness and vice versa. On the other hand, if I want to do both, it’s always possible.
I agree that it’s more complicated than it needs to be… But when I have an idea in mind, I go through with it.
Although it’s possible for the default brightness value to be the light’s existing brightness value, that’s only possible if the light is already on so it’s not a good fit for your application.