Eduardo
(Eduardo)
January 14, 2025, 9:03pm
1
I’m trying to build an automation and I have several variables.
I want that, depending on the time and day, the output is one thing or another.
I have used this variable
is_day_time: “{{ now().weekday() in range(0, 5) and now().hour >= 8 and now().hour < 24 }}”
Time 1 would be from Monday to Friday between 8 to 24
Time 2 would be mon-fri 24 to 8 and weekends
Does that variable work as intended? I think is not working
123
(Taras)
January 14, 2025, 9:25pm
2
Copy-paste the following template into the Template Editor then change the values of the two variables to exercise the template and confirm it reports true
only when you want.
{% set today = 2 %}
{% set hour = 9 %}
{{ 0 <= today <= 4 and 8 <= hour <= 23 }}
If it works to your satisfaction, you can use this:
{{ 0 <= now().weekday() <= 4 and 8 <= now().hour <= 23 }}
EDIT
Correction. Fixed upper bound of first arithmetic comparison. See Mayhem_SWE’s post below.
Mayhem_SWE
(Magnus Helin)
January 14, 2025, 9:26pm
3
Why would you think it is not working? Seems fine to me. You can simplify the last two conditions:
{{ now().weekday() in range(0, 5) and 8 <= now().hour < 24 }}
Or use a schedule helper , which would allow you to an easy UI to edit activation times day by day.
Mayhem_SWE
(Magnus Helin)
January 14, 2025, 9:31pm
4
Small correction, range(0, 5)
is unintuitive and actually produces a list that does not include the ending value, so should be <= 4
1 Like
123
(Taras)
January 14, 2025, 9:40pm
5
Ouch! I knew that yet just glossed over it. Thanks for bringing it to my attention. I’ll correct the example.
Eduardo
(Eduardo)
January 14, 2025, 10:09pm
6
Thanks!
This was part of the variables in this automation
variables:
grid_power: "{{ states('sensor.shellyemg3_8cbfea96acd8_em1_power') | int(0) }}"
total_consumption: "{{ grid_power }}"
max_grid_power_day: 4700
max_grid_power_night: 6200
charge_current: "{{ states('number.corriente_de_carga') | int(0) }}"
charger_consumption: "{{ charge_current * 230 }}"
is_day_time: "{{ now().weekday() in range(0, 5) and 8 <= now().hour < 24 }}"
actual_free_power_day: "{{ max_grid_power_day - (total_consumption - charger_consumption) }}"
actual_free_power_night: "{{ max_grid_power_night - (total_consumption - charger_consumption) }}"
actual_free_power: |-
{% if is_day_time %}
{{ actual_free_power_day }}
{% else %}
{{ actual_free_power_night }}
{% endif %}
With the following target
target:
entity_id: number.corriente_de_carga
data:
value: |-
{% if actual_free_power >= 7360 %}
32
{% elif actual_free_power >= 7130 %}
31
{% elif actual_free_power >= 6900 %}
30
{% elif actual_free_power >= 6670 %}
29
{% elif actual_free_power >= 6440 %}
28
{% elif actual_free_power >= 6210 %}
27
{% elif actual_free_power >= 5980 %}
26
{% elif actual_free_power >= 5750 %}
25
{% elif actual_free_power >= 5520 %}
24
{% elif actual_free_power >= 5290 %}
23
{% elif actual_free_power >= 5060 %}
22
{% elif actual_free_power >= 4830 %}
21
{% elif actual_free_power >= 4600 %}
20
{% elif actual_free_power >= 4370 %}
19
{% elif actual_free_power >= 4140 %}
18
{% elif actual_free_power >= 3910 %}
17
{% elif actual_free_power >= 3680 %}
16
{% elif actual_free_power >= 3450 %}
15
{% elif actual_free_power >= 3220 %}
14
{% elif actual_free_power >= 2990 %}
13
{% elif actual_free_power >= 2760 %}
12
{% elif actual_free_power >= 2530 %}
11
{% elif actual_free_power >= 2300 %}
10
{% elif actual_free_power >= 2070 %}
9
{% elif actual_free_power >= 1840 %}
8
{% elif actual_free_power >= 1610 %}
7
{% elif actual_free_power >= 1380 %}
6
{% else %}
0
{% endif %}
action: number.set_value
But it’s not working. I had it working before adding the two time periods, so I though the issue was with that time template