91JJ
(91JJ)
March 20, 2022, 11:26am
1
I have two Helpers set up for Time that are used to determine whether light automations should run.
(so the lights will start turning on at 17:00 and stop at 09:00)
input_datetime.lights_automation_lights_auto_on_after
input_datetime.lights_automation_lights_auto_on_before
I’m wanting to create a sensor that will be ‘ON/TRUE’ when within these times, but ‘OFF/FALSE’ when not.
I’m not sure whether to create another Helper (toggle) and an automation to turn it on/off
(rather than a template sensor)
Next goal will be to add another Toggle Helper to determine whether to use these times OR the Sun Rise / Sun Set to run the Light Automations
123
(Taras)
March 20, 2022, 11:50am
2
I suggest using a Template Binary Sensor .
template:
- binary_sensor:
- name: Whatever
state: >
{{ now() <= today_at(states('input_datetime.lights_automation_lights_auto_on_before')) or
now() >= today_at(states('input_datetime.lights_automation_lights_auto_on_after')) }}
EDIT
Correction. Forgot to use states()
function.
91JJ
(91JJ)
March 20, 2022, 11:57am
3
Thanks!
Getting this error when trying the template?
123
(Taras)
March 20, 2022, 12:06pm
4
That’s due to my mistake. I forgot to include the states()
function in the template. I have corrected the example posted above.
1 Like
91JJ
(91JJ)
March 20, 2022, 12:39pm
5
Ah… That did the trick!
These are only additions, I’ll try work it out for myself but there’s more experienced people here
I’ve created a new Toggle to select whether to use the 'Sun Time’s
input_boolean.light_automation_times_use_sun
So next steps would be
If the Boolean is on, then the Template should check the “Sun Times” to return on/off
EDIT: Added icon template
template:
- binary_sensor:
- name: light_automation_running
state: >
{{ now() <= today_at(states('input_datetime.lights_automation_lights_auto_on_before')) or
now() >= today_at(states('input_datetime.lights_automation_lights_auto_on_after')) }}
icon: >
{% if is_state("binary_sensor.light_automation_running", "on") %}
mdi:lightbulb-group
{% else %}
mdi:lightbulb-group-off
{% endif %}
123
(Taras)
March 20, 2022, 1:43pm
6
Do you mean something like this?
state: >
{{ (now() <= today_at(states('input_datetime.lights_automation_lights_auto_on_before')) or
now() >= today_at(states('input_datetime.lights_automation_lights_auto_on_after'))) and
is_state('input_boolean.use_sun', 'on') }}
91JJ
(91JJ)
March 20, 2022, 7:29pm
7
No sorry, I mean more like an else statement… like
“If Boolean on, use Sun Times, if boolean off, use other times”
123
(Taras)
March 20, 2022, 9:33pm
8
What are the “other times”?
91JJ
(91JJ)
March 21, 2022, 10:04am
9
123:
Sorry, the code is bad, but something like this…
state: >
{% if is_state('input_boolean.use_sun', 'on') %}
(now() <= today_at(states('sensor.sun_next_set')) or
now() >= today_at(states('sensor.sun_next_rise')))
{% else %}
(now() <= today_at(states('input_datetime.lights_automation_lights_auto_on_before')) or
now() >= today_at(states('input_datetime.lights_automation_lights_auto_on_after')))
{% endif %}
123
(Taras)
March 21, 2022, 11:56am
10
If you’re interested, here’s another way of doing the same thing:
state: >
{% set use_sun = is_state('input_boolean.use_sun', 'on') %}
{{ now() <= today_at(states(iif(use_sun, 'sensor.sun_next_set', 'input_datetime.lights_automation_lights_auto_on_before'))) or
now() >= today_at(states(iif(use_sun, 'sensor.sun_next_rise', 'input_datetime.lights_automation_lights_auto_on_after'))) }}
EDIT
Correction. Removed leading (
1 Like
91JJ
(91JJ)
March 21, 2022, 12:09pm
11
123:
{% set use_sun = is_state('input_boolean.use_sun', 'on') %}
{{ (now() <= today_at(states(iif(use_sun, 'sensor.sun_next_set', 'input_datetime.lights_automation_lights_auto_on_before'))) or
now() >= today_at(states(iif(use_sun, 'sensor.sun_next_rise', 'input_datetime.lights_automation_lights_auto_on_after'))) }}
Yep, exactly like that…
But doesn’t work I added the extra ‘)’ that was missing…
Now getting this… Think I need to fix the sun rise/set template sensor as it’s cutting off the zero’s
- platform: template
sensors:
sun_next_rise:
friendly_name: "Sun - Next Rise"
value_template: >-
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H.%M') }}
- platform: template
sensors:
sun_next_set:
friendly_name: "Sun - Next Set"
value_template: >-
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%H.%M') }}
Here it has the zero on the end…
But in the card it doesn’t…
123
(Taras)
March 21, 2022, 1:38pm
12
Your two Template Sensors need to produce valid time strings in order to work with today_at
.
This is a valid time string:
18:20
This is not:
18.2
From the documentation:
1 Like
91JJ
(91JJ)
March 21, 2022, 2:26pm
13
Yep, I’m trying to work on these two templates.
As you say it isn’t valid but when I check in the Template Editor it works
- platform: template
sensors:
sun_next_rise:
friendly_name: "Sun - Next Rise"
value_template: >-
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H.%M') }}
- platform: template
sensors:
sun_next_set:
friendly_name: "Sun - Next Set"
value_template: >-
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%H.%M') }}
123
(Taras)
March 21, 2022, 2:33pm
14
Not sure what you are checking but if you paste this into the Template Editor it will fail because the time string is invalid.
{{ today_at(as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H.%M')) }}
But if you replace the period with a colon, it produces a valid time string and the template produces a valid datetime object.
{{ today_at(as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H:%M')) }}
1 Like
91JJ
(91JJ)
March 21, 2022, 3:59pm
15
That was it!
Finally working, thanks so much for your time and experience on this
ygreq
(ygreq)
July 23, 2022, 10:49pm
16
Hi folks! First time experimenting with a template. I tested this in the Developer Tools / Template section and it works.
{{today_at(states('input_datetime.valve_1_morning_begin')) < now() <= today_at(states('input_datetime.valve_1_morning_end'))}}
But I have no idea how to format it to work in HA.I edited the configuration.yaml like this but I cannot see the sensor. What am I doing wrong? Thank you so much!!
PS: The whole idea is to have 2 settable time values for morning irrigation routine. And detect if we are in that time period or not.
1 Like
ygreq
(ygreq)
July 23, 2022, 11:02pm
17
Huh? I commented out id and now it works. Sorry for the disturbance. I will leave the previous comment as it might help someone.
Still curious why it did not work with id.