Help with data_template automation

Hi I have the below automation rules set for my main door light to turn on & off:

# Front Light On 15min after sunset
- id: front_lights_on_at_sunset
  alias: front lights on at sunset
  trigger:
    platform: sun
    event: sunset
    offset: "00:15:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.maindoor

# Front Lights Off at 11PM
- id: front_lights_off_at_11_night
  alias: front lights off at 11 night
  trigger:
    platform: time
    at: "23:00:00"
  action:
    service: homeassistant.turn_off
    entity_id: switch.maindoor

I need help to implement data_ template to dynamically compute light off time based on season value. I have sensor.season configured and it provides values {‘Spring’, ‘Summer’, ‘Autumn’, ‘Winter’} and for each of these season I want light to turn off at {“22:30:00”, “23:30:00”, “23:00:00”, “22:00:00”}. Any help along this or any other suggestion appreciated.

Why can’t you just use the sun as it already knows the seasons :stuck_out_tongue:

Because the sun has been down for 4 hours by the time he wants to switch the lights off?

Off the top of my head the easiest way would be…

# Front Lights Off at time defined by season 
- id: front_lights_off_by_season
  alias: front lights off by season 
  trigger:
    platform: time
    at: "21:59:00"
  action:
    - delay: >
         TEMPLATE HERE - if winter, 1 minute, if spring 31 minutes etc

    - service: homeassistant.turn_off
      entity_id: switch.maindoor

I’m on my phone so you’ll have to figure out the template :+1:

Errm the sun knows where it is regardless of when he wants to turn the lights off or on

He wants to switch them off at a specific time based on the season, which is absolutely bugger all to do with the sun.

Errm its based on the season, which is based on when the sun is in the sky as far as I know :stuck_out_tongue:

Propose some code that achieves what he’s asked for using the sun then. People come here for help, not sarcasm.

Well lets see, we want to turn off a light dependent on seasons, which is dependent on where the SUN is in the sky, without being too sarcastic, i would just delay the light turn off by 3min 48s per day, so use the variable custom component to do the work, or just delay the turn off by 6hrs from sunset, however since the OP only wants an 1:30hrs diff between summer and winter maybe @anon43302295 ‘s idea is better than mine (can’t see any code there tho’ :wink: )

Thanks to both of you for your prompt response and sharing different ways of achieving it. I was trying to achieve through data_template but quickest one is to use the Keith’s idea the same sun set event with a standard delay to switch off the light and it will automatically adjust 3min 48 seconds per day. I also like the idea of delay calculation based on template as it opens the door for me to think of other automations. Can you please help with the exact syntax of template to use delay in below statement with {%if ( ) %} {%else ( ) %} …{endif%}? It is very confusing to me.

action:
    - delay: >
         TEMPLATE HERE - if winter, 1 minute, if spring 31 minutes etc

One again thanks for your help.

1 Like

I think there may be some confusion on what you really want to do.

do you want to always turn the lights off a certain number of hours or minutes after sunset (ie 4 hours)?

Do you want to turn the lights off at a certain time depending on the season for no regard to how long they are on?

the first is fairly easy. use an offset past sunset (offset: “04:00:00”)

the second is harder. I don’t know of a sensor for “season”. there might be one but i just don’t know it. or you could possibly create one based on date.

if it’s the second you just need to use if/else statements determined by your season sensor.

if it’s some other third thing then clarify, please.

- platform: season

In sensors.yaml :slight_smile:

1 Like

cool!

thanks!

always so much to learn. the more you figure out the more you find out you don’t know.

Yes I want to use Homeassistant’s inbuilt component Season that has a sensor which returns the value of season prevailing and take decision to switch off light specially because I am in EST zone that has big variation of time from summer to winter during the year. This is how I want light to turn off light:
Summer: 11:30PM
Autumn: 11:00PM
Winter: 10:00PM
Spring: 10:30PM
Thank you.

for those “in the know” can you use a template in a trigger?

EDIT:

nevermind. I see you can use a value_template for the trigger.

In that case you can set the trigger to be different times based on the season.

I have no clue if this will work, but here goes…

delay:
minutes: >
              {% if states("sensor.season") == 'Winter' %} 
                  1 
              {%  elif states("sensor.season") == 'Spring' %}
                  31 
              {%  elif states("sensor.season") == 'Summer' %}
                  91 
              {%  elif states("sensor.season") == 'Autumn' %}
                  61 
              {% endif %}'

So this works in the template editor, not sure if it will work in delay tho’, but give it a try :slight_smile:

I would look at the variable idea as well but atm I can’t get it working on my PC, but that’s another story :stuck_out_tongue:

1 Like

I was not able to place delay inside trigger action as it kept giving error instead tried the time directly inside trigger as below:

# Front Lights Off at Night
- id: front_lights_off_at_night
  alias: front lights off at night
  trigger:
    platform: time
    at: '{% if states("sensor.season") == "Winter" %}21:30:00{%  elif states("sensor.season") == "Spring" %}22:30:00{%  elif states("sensor.season") == "Summer" %}23:59:59{%  elif states("sensor.season") == "Autumn" %}23:00:00{% endif %}'
  action:
    service: homeassistant.turn_off
    entity_id: switch.maindoor

But config log giving error and can not figure out syntax error:
Invalid time specified: {% if states("sensor.season") == "Winter" %}21:30:00{% elif states("sensor.season") == "Spring" %}22:30:00{% elif states("sensor.season") == "Summer" %}23:59:59{% elif states("sensor.season") == "Autumn" %}23:00:00{% endif %} for dictionary value @ data['trigger'][0]['at']. Got None. (See /config/configuration.yaml, line 299). Please check the docs at https://home-assistant.io/components/automation/_

Try placing the time element in single quotes - ‘21:30:00’, for each element.

I’ll keep searching for the delay way , I’m fairly sure it can be done.

Ok, so you can do it like this …

- delay: '00:{{ states.input_select.tiempo_calefactor.state | int }}:00'

so I think the way to do it is by using a sensor to create the value and then placing the sensor into the delay as above.

sensor:
  lamp_delay:
     value_template: > 
       {% if states("sensor.season") == 'Winter' %} 
           "01" 
       {%  elif states("sensor.season") == 'Spring' %}
           "31" 
       {%  elif states("sensor.season") == 'Summer' %}
           "91" 
       {%  elif states("sensor.season") == 'Autumn' %}
           "61" 
       {% endif %}

- delay: '00:{{ states.sensor.lamp_delay.state | int }}:00'

2 Likes

Change this to “01”

Thanks Bob :slight_smile:

1 Like