Multiple Template Rule based on times for percentage

I have several helpers set up to determine the percentage the lights should be turned on.
I’ve labelled this as “AM” and a “PM” times and percentage.

I’m trying to set up a template that will only show the percentage based on the time.
IF between “AM Start” and “AM End” = display 20%
IF between “PM Start” and “PM End” = display 55%
Else = Display 100%

AM Entities

{{ states('input_number.lights_brightness_am') }}
{{ states('input_datetime.lightautomation_am_bri_start') }}
{{ states('input_datetime.lightautomation_am_bri_end') }}

PM Entitties

{{ states('input_number.lights_brightness_pm') }}
{{ states('input_datetime.lightautomation_pm_bri_start') }}
{{ states('input_datetime.lightautomation_pm_bri_end') }}

Use conditional card feature:

and create one sensor to determine if you are in AM for example. You display the required card (using two conditional cards) if the state of the sensor is “on” (AM card) or “off” (PM Card)…

I believe the template could be:


it’s currently 8:37

{{ today_at("07:30") < now() < today_at("21:59") }}  

Personally I would make this a binary sensor so that it would be on/off and that should then control the conditional card.
Doing the templating past midnight would be to much work for me.

Thanks both.
I agree id like to use a binary sensor.

I have a very similar thread here that I’d like to replicate

I think these are working to return ‘true’ or ‘false’. I just need to return the percentage if true…

{{ now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) or
   now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) }}

{{ now() <= today_at(states('input_datetime.lightautomation_pm_bri_end')) or
   now() >= today_at(states('input_datetime.lightautomation_pm_bri_start')) }}

But one sensor is enough?

If you just have the yaml I wrote then it will be true at daytime and false at night, that is enough for your conditional card.
No need to make two binary sensors.

I’m wanting to use those to create a Template Binary Sensor which will show the % based off the am/pm times.

image

AM : {{ states('input_number.lights_brightness_am') }}%
{{ states('input_datetime.lightautomation_am_bri_start') }}
{{ states('input_datetime.lightautomation_am_bri_end') }}

PM : {{ states('input_number.lights_brightness_pm') }}%
{{ states('input_datetime.lightautomation_pm_bri_start') }}
{{ states('input_datetime.lightautomation_pm_bri_end') }}

Time Now: {{now().hour }}:{{now().minute}}

AM % : {{ now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) or
          now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) }}

PM % : {{ now() >= today_at(states('input_datetime.lightautomation_pm_bri_start')) or
          now() <= today_at(states('input_datetime.lightautomation_pm_bri_end')) }}

Here’s a really bad example of what I’m trying to do…

'TEMPLATE NAME = LIGHT_PERCENTAGE'
IF 
 {{ now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) or
          now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) }}
= "TRUE"
THEN
{{ states('input_number.lights_brightness_am') }}%
ELSE
{{ states('input_number.lights_brightness_pm') }}%
ELSE IF
100%
END
}}

Yes and that is what I mean, create a binary sensor:

configuration.yaml:

template:
  - binary_sensor:
      - name: "PM now"
        state: "{{ today_at(states('input_datetime.lightautomation_pm_bri_start')) < now() < today_at(states('input_datetime.lightautomation_pm_bri_end')) }}"

Card1:

type: conditional
conditions:
  - entity: binary_sensor.pm_now
    state: "on"
card:
  type: entities
  entities:
    - input_number.lights_brightness_pm

Card2

type: conditional
conditions:
  - entity: binary_sensor.pm_now
    state: "off"
card:
  type: entities
  entities:
    - input_number.lights_brightness_am

Or you could have a look at the :small_blue_diamond: state-switch - conditional card on steroids - Lovelace & Frontend - Home Assistant Community (home-assistant.io)

1 Like

Thanks!

I like that way, but still restricts me as I’m wanting to use the output to determine the percentage when turning on the lights.

I’m not sure I understand this then…
What is restricting you how?

Hi,

I guess your are more looking for one sensor that holds the brightness percentage to be used when turning the light on, and this varies depending on the time of the day, right?

If so it should be possible to define a template sensor that does the trick.
Something like this:

template:
  - sensor:
    - name: "current multiperiod light percentage"
      state: >
        {% if now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) or
           now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) %}
          {{ states('input_number.lights_brightness_am') }}
        {% elif now() >= today_at(states('input_datetime.lightautomation_pm_bri_start')) and
           now() <= today_at(states('input_datetime.lightautomation_pm_bri_end')) %}
          {{ states('input_number.lights_brightness_pm') }}
        {% else %}
          100
        {% endif %}
      unit_of_measurement: '%'

and is used if the period is with in the same date, while or is used for periods that crosses a date.

I this case we know that the am period crosses a date (starting in the evening of one day and ending in the morning of next day) and that is why or is used, while and is used between the start and end conditions of the am period.

Please let me know if this works for you.

Kind regards,
Ghassan

Yep… Exactly something like this!

It’s weird as it works with the PM numbers, but not the AM.
I’ve tried tweaking the code to check the times both ways but still isn’t working right.

So when you write display

That is not display, that is set the light brightness to…
How are we supposed to know that :man_facepalming:

yeah sorry, should have read “return” not “display”

Help?
image

Seems like % should be surrounded by '' like this '%' :slight_smile:

Try this version:

  - sensor:
    - name: "current light percentage"
      state: >
        {% if now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) or
          now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) %}
          {{ states('input_number.lights_brightness_am') }}
        {% elif now() <= today_at(states('input_datetime.lightautomation_pm_bri_end')) or
          now() >= today_at(states('input_datetime.lightautomation_pm_bri_start')) %}
          {{ states('input_number.lights_brightness_pm') }}
        {% else %}
          100
        {% endif %}
      unit_of_measurement: '%'

I removed this and config is now valid :smiley:

        unit_of_measurement: %

Great :slight_smile:

Tried to replicate your setup. I am a bit confused:

input_datetime.lightautomation_am_bri_start = 22:00 is that 10 PM?
input_datetime.lightautomation_pm_bri_start = 7:30 is that 7:30 AM?

so am period starts at 10 PM and pm period starts at 7:30 AM, then the helper naming is probably confusing.

Anyway, if you are not intending to have a gap between the am and pm brightness periods, then I’d rather suggest that you only have the start of each period. The end is defined by the start of the other period :slight_smile:

Then the code can be simplified like this:

  - sensor:
    - name: "current light percentage"
      state: >
        {% if now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) and 
          now() < today_at(states('input_datetime.lightautomation_pm_bri_start')) %}
          {{ states('input_number.lights_brightness_am') }}
        {% else %}
          {{ states('input_number.lights_brightness_pm') }}
        {% endif %}
      unit_of_measurement: '%'
1 Like

My intention was to have two periods where I could customise the light percentage.

Between these times, the percentage would be low (currently 20%)
input_datetime.lightautomation_am_bri_start = 22:00 (10pm)
input_datetime.lightautomation_am_bri_end = 07:29 (7:29am)

Between these times, the percentage would be medium (currently 55%)
input_datetime.lightautomation_pm_bri_start = 07:29 (7:29am)
input_datetime.lightautomation_pm_bri_end = 22:00 (10pm)

These are currently covering the 24hours, however, on my automations, if these times fail then it defaults to 100%… So I suppose the naming is very confusing (sorry :frowning:)

I added AM & PM ‘Brightness / times’ as I personally found two times that I wanted to control, everything else can be 100%.

1 Like

Sorry I keep writing, but the following version of the template is more correct.
The other one is not precise and can give wrong results in some cases;:

should use and instead of or

 state: >
        {% if now() >= today_at(states('input_datetime.lightautomation_am_bri_start')) and
           now() <= today_at(states('input_datetime.lightautomation_am_bri_end')) %}
          {{ states('input_number.lights_brightness_am') }}
        {% elif now() >= today_at(states('input_datetime.lightautomation_pm_bri_start')) and
           now() <= today_at(states('input_datetime.lightautomation_pm_bri_end')) %}
          {{ states('input_number.lights_brightness_pm') }}
        {% else %}
          100
        {% endif %}