Input boolean set using sun and time

I want an input boolean to be true if sun is below horizon with an offset AND time is between 22:00 and 05:00. If one of these conditions is not true the boolean must be false.

I would like to only use one automation for this. Is this possible and if yes how?

Does this need to be an input boolean… i.e. do you need to maintain the ability to manually turn it on and off? If not, then you can do it with 0 automations by creating a Template binary sensor.

Is your offset time-based or solar elevation-based?

I will only use it as condition in an automation and don´t need to turn it on and off manually.
How would this template look?

It is solar elevation based.

What’s the value of the offset? For example, 15 minutes after sunset?

template:
  - binary_sensor:
      - name: Sun and Time
        state: "{{ state_attr('sun.sun', 'elevation') > -6 and 5 <= now().hour < 22  }}"
        availability: "{{ has_value('sun.sun') }}"

The offset is solar elevation based not time based.

I tried putting it like this when creating the binary sensor it but returns an error
I have all my binary inputs in a dedicated yaml file.

  - platform: "template"
    name: "sun and time"
    state: "{{ state_attr('sun.sun', 'elevation') > -5.5 and 5 <= now().hour < 22  }}"
    availability: "{{ has_value('sun.sun') }}"

Errors often are self-explanatory, so it would be better when you post the error here.

It looks like you are still working in the legacy sensor format, and the code from Didgeridrew is in the modern format.
In your case it probably should be something like:

binary_sensor:
  - platform: template
    sensors:
      sun_and_time:
        friendly_name: "Sun and Time"
        value_template: "{{ state_attr('sun.sun', 'elevation') > -5.5 and 5 <= now().hour < 22  }}"
        availability_template: "{{ has_value('sun.sun') }}"

That helped, no error anymore :slight_smile:
However it seems to be ON right now which shouldn´t be the case.
Only if sun is below -5.5 and time is between 22:00 and 05:00

Shouldn´t it be
< -5.5 and 22 <= now().hour < 5 }}" ?

Great!
Yes, with the current template the sensor is ON (or true) when the sun elevation is above -5.5 deg and the time is between 05:00 and 22:00, so in any other case it is OFF.
You could argue “what’s in a name”, and can easily use this for any further usage by selecting OFF instead of ON, but indeed your proposal inverts the template behavior.

You’re going to need an or for your times… the hour value will never be both greater than 22 and less than 5.

{{ state_attr('sun.sun', 'elevation') < -5.5 and (5 > now().hour or now().hour >= 22) }}

For future reference:

For a Sun Trigger, the word offset refers to time. That’s why I assumed you were referring to time when you said ‘sun is below horizon with an offset’.

In contrast, when discussing the sun’s elevation, as when used with a Sun Elevation Trigger, there’s no reference to ‘offset’. The sun’s elevation is merely a positive (above horizon) or negative (below horizon) value.

That’s why both Didgeridrew and I asked for clarification about your use of the word ‘offset’. In a template, it’s easier to use the sun’s elevation than performing time arithmetic with sunset or sunrise. Had ‘offset’ been time, the template would have been a bit longer.

Okay I will try that.
Actually I just noticed this in the logs ( it does not return an error on the frontend though)

2023-08-09 15:08:42 WARNING (MainThread) [homeassistant.components.template] Template binary sensor ‘sun_and_time’ has no entity ids configured to track nor were we able to extract the entities to track from the availability template(s). This entity will only be able to be updated manually.

2023-08-09 15:08:54 ERROR (MainThread) [homeassistant.components.template.binary_sensor] Could not render available template sun_and_time: UndefinedError: ‘has_value’ is undefined

Copy-paste the following template into the Template Editor and confirm it reports True.

{{ has_value('sun.sun') }}

If it doesn’t then it implies the Sun integration isn’t installed (which would be surprising).

Actually I am still on version 0.103.6 so maybe that is why.
Template editor shows “Error rendering template: UndefinedError: ‘has_value’ is undefined”

Ok… for future reference, you should start with that information. You’re more than 3 years behind the current version and there have been significant changes to templates (as well as nearly every other integration) since then.

 availability_template: "{{ states('sun.sun') not in ['unavailable', 'unknown'] }}"
1 Like

I see you like antiques. :slightly_smiling_face:

1 Like

I would encourage you to update.

1 Like

Yes I forgot to mention at start… sorry
Using availability_template: "{{ states('sun.sun') not in ['unavailable', 'unknown'] }} returns an error however.

2023-08-09 17:24:36 ERROR (MainThread) [homeassistant.components.homeassistant] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning a quoted scalar
in “/home/homeassistant/.homeassistant/binary_sensors.yaml”, line 424, column 32
found unexpected end of stream
in “/home/homeassistant/.homeassistant/binary_sensors.yaml”, line 424, column 90

I really want to update as soon as I get some more spare time. Some integrations does not work anymore, some new stuff can´t be integrated and so on, but I think most of my existing yaml programming won´t work anymore and needs to be redone afterwards.
But updating is definitely on my todo list.