Hi, I got stuck and unable to resolve the issue on my own, many thanks in advance for any help, much appreciated.
I created a script to toggle or turn on light but I cannot turn off light with this script because when I use light.turn_off for the {{ lightmode }} brightness_pct should be omitted but I cannot add if statement. I tried many things but most of the time I get error :“Error rendering data template: Result is not a Dictionary”
Thanks tom_I and Taras
your code helped me move in the right direction and both of your solutions worked.
Think that your solution tom_I is more flexible since it allows for more control with ifs due to some HA limitations.
I had some time to thinker with the script and this is what I got in the end to do what i need for the moment, turn on lights different brightness at night and day with same script and allow for flexibility to exclude some lights. Here is the final script:
You’re welcome and I appreciate your comment but, honestly, your latest example appears to contain little of what I had suggested.
There’s no need to pass the entire service call in lightmode. The script is already constrained to using a light-based service call due to its use of brightness_pct. The value of lightmode only needs to be either on or off.
Similarly, there’s no need for entity to contain the domain (light) because the script is limited to light-based entities. The value of entity only needs to contain the light’s object_id (i.e. object_id is everything to the right of the period in light.living_room).
Your script basically sets a light’s brightness to either 0, 10, 21, or 90 based on the time and/or if it’s the kitchen light. All of that can be templated in a variable and passed to a single service call
In the following example, all of the “thinking” is performed by the bp variable. If lightmode is off then the value is set to zero. Otherwise, if the current time is not within the specified range, the value is 90. Otherwise it’s 21 if it’s the kitchen light or 10 for any other light.
alias: lights_day90_night10
sequence:
- variables:
in_range: "{{ (now().hour, now().minute) < (7, 30) or now().hour >= 21 }}"
bp: "{{ 0 if lightmode == 'off' else 90 if not in_range else 21 if entity == 'kitchen1' else 10 }}"
- service: light.turn_on
target:
entity_id: "{{ entity }}"
data:
brightness_pct: "{{ bp }}"
transition: 1
mode: parallel
max: 24
Hi Taras
You are right I have not used exactly your code , but your example helped me to move from dead end sort of speak.
Thank you for the latest script, I will examine and test it over the weekends and have some feedback.