Hi folks,
I would like an actuator to be switched on 16 hours a day, always during the day.
now I want to convey the switching times so that they are exactly between sunrise and sunset.
How can I set that if the time span between sunrise and sunset was only 12 hours, for example,
the switch-on time is automatically two hours earlier and the switch-off time two hours later and this adapts dynamically to the astrological values?
1 Like
Hellis81
(Hellis81)
February 25, 2022, 6:55am
2
You have next_noon:
So you can probably use something like this:
noon: {{ state_attr('sun.sun','next_noon') }}
- 8 hours: {{ (state_attr('sun.sun','next_noon') | as_datetime) - timedelta(hours=8) }}
+ 8 hours: {{ (state_attr('sun.sun','next_noon') | as_datetime) + timedelta(hours=8) }}
1 Like
tom_l
February 25, 2022, 6:59am
3
The easiest way would be to use solar noon (halfway between sunrise and sunset). Switch on 8 hours before solar noon and switch off 8 hours after solar noon. You have to be careful when you calculate this though. After solar noon the next solar noon is tomorrow. Probably best to calculate it at one am with a triggered template sensor.
template:
- trigger:
- platform: time
at: '01:00:00'
sensor:
- name: "On Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8) }}"
device_class: timestamp
- name: "Off Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8) }}"
device_class: timestamp
You can then use these sensors in time triggers for your automations.
EDIT: I was beaten to this by Hellis, but a couple of things to note they missed:
The need to do the calculation before solar noon.
The need to convert the times to local time.
The need to use a timestamp device class, or the sensors can not be used in time triggers.
3 Likes
tom_l:
template:
- trigger:
- platform: time
at: '01:00:00'
sensor:
- name: "On Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8) }}"
device_class: timestamp
- name: "Off Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8) }}"
device_class: timestamp
Unfortunately I donât know how to work with these new sensors.
I tried this, but it doesnât work:
alias: Pflanzenlampe ausschalten
description: ''
trigger:
- platform: time
at: sensor.growbox_off
- platform: time
at: '00:00'
condition: []
action:
- type: turn_off
device_id: ff0670171d477db7db7417d1508d1314
entity_id: switch.shelly_wallplug
domain: switch
mode: single
I think the problem is that the new sensor is a complete timestamp and not only the clocktime
tom_l
February 25, 2022, 11:08pm
5
It should not matter but just in case, try this:
template:
- trigger:
- platform: time
at: '01:00:00'
sensor:
- name: "On Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8)).strftime('%H:%M:%S') }}"
- name: "Off Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8)).strftime('%H:%M:%S') }}"
this results in âunknownâ
tom_l
February 26, 2022, 8:14am
7
Has it been 1 am yet?
That is when the times are calculated. Until then it will be unknown.
Thereâs nothing wrong with the templates:
If you donât care about the minutes difference between days you can do this:
template:
- sensor:
- name: "On Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8)).strftime('%H:%M:%S') }}"
- name: "Off Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8)).strftime('%H:%M:%S') }}"
it will update every minute but after solar noon the off time will change to that calculated for tomorrow which could be minutes different from todayâs actual off time.
Yesterday the values got updated after a restart.
I thought this would happen at every reboot.
Hellis81
(Hellis81)
February 26, 2022, 10:46am
10
Couldnât a home assistant start also be added as a trigger in case restart is done during the day?
Maybe Iâm stupid, but the way I use the sensor-value doesnât trigger my automation.
How do I have to configure the trigger?
tom_l
February 26, 2022, 10:59am
12
No, it must only trigger before solar noon or the off time will be for tomorrow. Which in practice is only a matter of minutes in most places in the world which is why I said they could probably use this instead which will update every minute:
template:
- sensor:
- name: "On Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8)).strftime('%H:%M:%S') }}"
- name: "Off Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8)).strftime('%H:%M:%S') }}"
Unless they live near the poles. On time wonât be an issue as it always occurs before solar noon.
Exactly as shown, this goes in your configuration.yaml file, not your automations.yaml file:
template:
- trigger:
- platform: time
at: '01:00:00'
sensor:
- name: "On Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8)).strftime('%H:%M:%S') }}"
- name: "Off Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8)).strftime('%H:%M:%S') }}"
See: https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors
Well I know that.
The Problem is, that I want to teigger my automation with these values.
I configured this trigger to my automation:
trigger:
- platform: time
at: sensor.on_time
âŚbut this doesnât work.
tom_l
February 26, 2022, 11:12am
14
Oh I see.
It should work according to the docs. https://www.home-assistant.io/docs/automation/trigger/#sensors-of-datetime-device-class
Did you try it with this?
sensor:
- name: "On Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8) }}"
device_class: timestamp
- name: "Off Time"
state: "{{ state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8) }}"
device_class: timestamp
tom_l
February 26, 2022, 11:17am
16
When the sensors had a value that was not unavailable
?
What about this:
template:
- trigger:
- platform: time
at: '01:00:00'
sensor:
- name: "On Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local - timedelta(hours=8)).strftime('%H:%M:%S') }}"
device_class: timestamp
- name: "Off Time"
state: "{{ (state_attr('sun.sun', 'next_noon')|as_datetime|as_local + timedelta(hours=8)).strftime('%H:%M:%S') }}"
device_class: timestamp
Yes, I removed the 01:00:00 trigger
Both sensors have a valid timestamp
I edited them at the developer tools to test them
I get an error when I save the automation
key not provided @ 'at' bla bla
Hellis81
(Hellis81)
February 26, 2022, 12:34pm
19
Donât you think the actual error message is quite important?
That doesnât give us much.
Hellis81
(Hellis81)
February 26, 2022, 12:37pm
20
I understand that. But I meant as a backup.
Letâs sunrise is at 7:30 and the switch on is about that time also.
Then at 9 a restart is done, that means the sensor is unavailable and the switch off is not done.
Having both trigger at 1 am and home assistant start means if the sensor is cleared mid day we get a new value, âincorrectâ but still a value, that makes it switch off at night.
Otherwise it could be left on until the next evening.
This would give âaccurateâ times most of the time, and when a restart occurs it gives a off time that is reasonably correct.