Regenbui
(Wesley Westerveld)
March 1, 2023, 10:06am
1
Hi All,
I want to use an if then else statement to set the brightness for my light. I’m a bit in a lost . I’ve got the following statement tested succesfully in my developer tools >> templates
data_template: >
{% if now().hour < 12 %} 100
{% elif now().hour > 5 %} 50
{% else %}200
{% endif %}
What is the next step. I had a look at several youtube tutorials and they seem to talk about creating Sensor. Do I need to add this to my confiuration.yaml, so i can do a service call in my automation. Bit stuck here
Troon
(Troon)
March 1, 2023, 11:50am
2
Does your light already exist in HA? If so, you need an automation, something like:
alias: Light brightness control
id: bec3fdde-f252-4ce2-b328-31a9e2bf8598
trigger:
- platform: time_pattern
minutes: "0"
action:
- service: light.turn_on
target:
entity_id: light.your_light
data:
brightness: >
{% if now().hour < 12 %} 100
{% elif now().hour > 5 %} 50
{% else %} 200
{% endif %}
Rofo
(Ro)
March 1, 2023, 1:29pm
3
Also, you conditional logic looks flawed.
brightness: >
{% if now().hour < 12 %} 100
{% elif now().hour > 5 %} 50
{% else %} 200
{% endif %}
The first if statement will always fire if the hour is less than 12.
However, this means the second statement actually is working like this:-
if hour>=12 … not >5, because the second elif will only trigger if the first if does not.
This does not appear to be what you are intending.
Troon
(Troon)
March 1, 2023, 1:40pm
4
Agreed: this gives 100 up to 12, then 50 — never 200:
{% for h in range(24)|list %}
{{ h }}: {% if h < 12 %}100{% elif h > 5 %} 50{% else %}200{% endif -%}
{% endfor %}
Regenbui
(Wesley Westerveld)
March 2, 2023, 9:55am
5
Thnx for the feedback. I’ve adjusted my code in YAML as below.
alias: New Automation
description: Testing
trigger:
- platform: time_pattern
minutes: "0"
condition: []
action:
- service: light.turn_on
target:
entity_id: light.werkkamer_plafondlamp_light
data:
brightness: >
{% if now().hour > 12 %} 100 {% elif now().hour < 12 %} 50 {% else %}
100 {% endif %}
mode: single
The weird thing is, that when I set my brightness to 100 and execute the above automatin, it sets it to 20. Any thoughts on this?
Step details give the below, which seems correct:
Executed: March 2, 2023 at 10:57:26
Result:
params:
domain: light
service: turn_on
service_data:
brightness: 50
entity_id:
- light.werkkamer_plafondlamp_light
target:
entity_id:
- light.werkkamer_plafondlamp_light
running_script: false
limit: 10
Troon
(Troon)
March 2, 2023, 10:06am
6
Feels like you’re guessing at the template code without fully understanding what’s going on. Your latest attempt is equivalent to:
brightness: "{{ 50 if now().hour < 12 else 100 }}"
I assume you’re aware that the brightness
ranges from 0 to 255 (ref )? Is it possible you’re setting it to 50 (out of 255) and reading it back as 20% elsewhere?
Rofo
(Ro)
March 2, 2023, 11:04am
7
I think the confusion comes when you use the UI, you can express brightness as either a percentage or a 0-255 value.
If you want to use brightness as a percentage then use:
- service: light.turn_on
data:
brightness_pct: 100
Also, your logic is still flawed This:
data:
brightness: >
{% if now().hour > 12 %} 100 {% elif now().hour < 12 %} 50 {% else %}
100 {% endif %}
Translates as the following:
13-23 hour, then brightness is 100
0-11 hour, then brightness is 50
12 hour, then brightness 100
Which again, is not what I think you are intending, or if it is, its slightly strange way of doing it, because it ultimately then becomes:
12-23 hour, then brightness is 100
0-11 hour, then brightness is 50
Which is probably what you ARE intending.