How can I convey switching times?

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

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

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:

  1. The need to do the calculation before solar noon.
  2. The need to convert the times to local time.
  3. The need to use a timestamp device class, or the sensors can not be used in time triggers.
3 Likes

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

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’

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.

Nope only at 1am.

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?

:confused:

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.

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

I tried both

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

Don’t you think the actual error message is quite important?
That doesn’t give us much.

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.