Template Sensor Help - Based between times

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)
image

  • 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.

  1. I’m not sure whether to create another Helper (toggle) and an automation to turn it on/off
    (rather than a template sensor)
  2. 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

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.

Thanks!

Getting this error when trying the template?

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

Ah… That did the trick!

These are only additions, I’ll try work it out for myself but there’s more experienced people here :slight_smile:

I’ve created a new Toggle to select whether to use the 'Sun Time’s

input_boolean.light_automation_times_use_sun

image

So next steps would be

  1. 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 %}

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') }}

No sorry, I mean more like an else statement… like

“If Boolean on, use Sun Times, if boolean off, use other times”

What are the “other times”?

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 %}

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

Yep, exactly like that…
But doesn’t work :blush: 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
image

- 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…
image

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

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') }}

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

That was it!
Finally working, thanks so much for your time and experience on this :slight_smile:

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.

time

1 Like

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.