I am using the Adaptive Lighting component to change the color temperature of my lights throughout the day. This component stores some settings in a switch entity. Other settings are user-configurable (through both the UI and YAML) but are stored at the component level; there is no entity associated with those settings.
Here’s an example YAML entry for the component-level settings. This would go in configuration.yaml.
adaptive_lighting:
- name: "default"
lights: []
transition: 45
initial_transition: 1
interval: 90
sunrise_time: "08:00:00" # override the sunrise time
Is there any way to set the sunrise_time value dynamically? I considered using a template, but I couldn’t find any examples or documentation of templates being used like that, and it didn’t work when I tried it with the similar Circadian Lighting add-on. I also considered using a script, but I can’t figure out how to get the sunrise_time value in a script, let alone set it.
Adaptive lighting is not in the list of home assistant integrations. Do you mean this one which can be added to HACS?
HA doesn’t allow users to provide templates for any configuration field in a template. A component/integration has to specifically enable template support in any of its configuration fields where it wants to allow templates. Assuming you mean the component I linked, I also don’t see a template option in the documentation so I would not assume its supported. I’d recommend submitting a feature request to the github repo of that component.
Although on a side note (again, assuming I have the right Adaptive Lighting component), I do see options for sunrise/sunsset offset. So rather then completely overriding the sunset time you can have it use a time a bit before or after the sunset/sunrise time, giving you some control but still keeping it dynamic. Does that fit your use case or do you really need a fully different time?
Adaptive lighting is not in the list of home assistant integrations. Do you mean this one which can be added to HACS?
Yes, sorry. I’ve updated the OP to include that.
HA doesn’t allow users to provide templates for any configuration field in a template. A component/integration has to specifically enable template support in any of its configuration fields where it wants to allow templates. Assuming you mean the component I linked, I also don’t see a template option in the documentation so I would not assume its supported. I’d recommend submitting a feature request to the github repo of that component.
I’m considering just forking and making a pull request. I know a tiny bit of Python, but it might be an easy change. The component schema calls for a string. Suppose I change the schema to call for a template that returns a string. Will it be processed any differently than a regular string would have been? In other words, will {{This template returns 10:00:00}} be treated just like 10:00:00 would have been?
Although on a side note (again, assuming I have the right Adaptive Lighting component), I do see options for sunrise/sunsset offset. So rather then completely overriding the sunset time you can have it use a time a bit before or after the sunset/sunrise time, giving you some control but still keeping it dynamic. Does that fit your use case or do you really need a fully different time?
That won’t work, unfortunately. The offsets are set in the same way that sunrise_time is. I have an automation that gets my phone’s next alarm and gradually turns on my lights before the alarm. I’m hoping to set the sunrise based on that alarm, which means it will vary from day to day.
Changing the schema is roughly that simple but there’s a bit more to it then that. So to start you can see this line at the top:
import homeassistant.helpers.config_validation as cv
This imports all the common validators people use throughout the schemas defined for integrations. There’s one for template you should use that validates what is entered is a template and also sets you up for rendering the template at runtime.
That second bit is the trickier part though. Because the key difference between a template and a static string is a string is a known and constant value from the time HA starts up to the time it shuts down. A template is not, it changes every time the state it depends on changes so it has to be recalculated frequently.
So what you then have to do is go find all the places in the code where that value is used and adjust them. Any place where you see it get that config value has to be adjusted to instead render the template at that moment in time. You will also need to introduce error handling for what happens if that template fails to render or if it renders but what you get does not look like a valid time string. The author was validating that the entered text did look like a valid time string in the schema, that’s no longer possible with a template.
I’d recommend picking an integration in core HA which supports templates in its config and taking a look at how it does it. Then you can do the same in your own.
Also one other thing to be aware of, I see that component has a config_flow.py and is therefore UI-editable. It also looks like the schema is shared between the UI and the YAML configuration processes. The UI has some limitations around what kinds of schema it supports so just make sure not to break that. You’ll probably have to do what the author did and put the template validation in the EXTRA_VALIDATION block for validation that runs afterwards but isn’t supported in the UI.
Actually thinking about it more you have to register listeners for this one as it should be state-driven, its not triggered by some kind of user input. If sunrise_time is a template then there’s not really a clear and obvious time to calculate that value by rendering the template. You can’t render it at a specific time of day because you might’ve missed when you were supposed to start.
So you might want to look specifically at how template sensor works or any of the core template integrations work. Because you will essentially need to make this state driven. It will need to keep track of which entities are referenced in that template and listen for updates to those entities. When they are updated you’ll need to re-render the template to update the sunrise time accordingly.
Thanks for the detailed response. Since I’ll have to rework the component anyway, it might make more sense to just move sunrise_time to an entity, so I can set it with automation. Then the rest of the logic can stay the same.