[Semi-noob] - How to create a template that is using time of day

Hi all.
Being a semi-noob, I am trying to improve my skills with at bit more advanced configuration in Home Assistant.
I need some help setting up my first template - and try to understand templates better.
So what would I like to achive?
I have a sensor for the font door.
I want the light to turn on at 50 percent 30 min. before sunset until 23:00.
From 23:00 until 6:00 in the morning, I want it to dim to 5-10 percent, and from 6 in the morning to 1 hour after sunrise it will go back to 50 percent.
Whenever there is movement it will go to 100 percent for 2 minutes after motion is no longer detected.

I allmost have it working using automation and choose, but it’s a bit complicated and is not that stable.
Furthermore I would like to be able to use the template in the future.
So basicly I need a template, that returns Evening, night and morning, depending on the above statements.
So far I have only been able to make at template going on or off.

Any advise would be very appreciated

Cheers in advance

alias: example
trigger:
  - platform: sun
    event: sunset
    offset: '-00:30:00'
    variables:
      level: 50
  - platform: time 
    at: '23:00:00'
    variables:
      level: 10
  - platform: time 
    at: '06:00:00'
    variables:
      level: "{{ 50 if states('sun.sun') == 'below_horizon' else 0 }}"
  - platform: sun
    event: sunrise
    offset: '01:00:00'
    variables:
      level: 0
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.your_light
    data:
      brightness_pct: "{{ level }}"

Create a separate automation to handle turning on the light to full brightness when the door is opened. I suggest creating a snapshot scene, before setting the light’s brightness to 100, so that it makes it easy to restore the light to its previous brightness. Let me know if you need my help to create this additional automation.

Hi Taras.
Thank You for your quick reply - really appriciate it.
However, I get an error, saying “expected float for dictionary value @ data[‘brightness_pct’]“
I have tried several ways to make the “{{ level }}” to float, but with no luck.
Someone has any pointers?
Thanks in advance

How are you testing the automation?

You can’t use the “Run” command because that doesn’t exercise the automation’s trigger section and only executes its actions.

This automation must be triggered by one of its four triggers because that’s when the level variable is defined. If only the action is executed, the level variable is undefined and will produce an error message (expected float for dictionary value @ data[‘brightness_pct’]“).

Reference: Testing your automation

I AM testing using the run command.
I will see how it work when we’re passing sunset today…
Thanks a lot

IT WORKS!
Thanks a lot for your explanations.
Really, really, REALLY appriciate it

1 Like