Turn on the lighting down the sun goes down

Hello,
try to turn on, the lighting when the sun goes down (+ the time it gets dark)
the lighting should be on for 3 hours.
after 3 hours the lighting should go off.
and the next day the same.
has made his own, has worked but not now. can anyone help me with that .
Thanks

> - alias: 'Turn Altan lights on when the sun gets dim'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: -4.00
  action:
    service: switch.turn_on
    entity_id: switch.telldus_tzwp_100_plug_in_switch_switch
- alias: 'Close blind Altan at dusk'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: -17.22
  action:
    service: switch.turn_off
    entity_id: switch.telldus_tzwp_100_plug_in_switch_switch

Change your triggers like this and see if it works:

trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: -4.00

trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: -17.22

Thanks. will try tonight

No it does not trigger, what about below if you want to light on after 1 hour after sunset, how does it work?

You could use the sunset/sunrise trigger with offset, see here.

I have tested the same code and it triggered for me. Do you get any errors in the logs? How did you test?

It might be able to work, but I can imagine it wonā€™t because it is not a specific trigger. Itā€™s more a condition.
It also matches below -18 for example. But as said, it might work because HA is pretty clever from time to time.

Anyway, Iā€™d try using a template trigger with a fixed ā€œtrueā€ match, for example:

{{ state_attr('sun.sun', 'elevation') == -17  }}

or

{{ is_state_attr('sun.sun', 'elevation', -17.22)  }}

If true, then it triggers

No, it is a specific trigger. Trigger when elevation goes below -17.22. This will trigger whenever the elevation goes from above -17.22 to below -17.22. If you are already at e.g. -18 and it changes to -19, it wonā€™t trigger, it only triggers when it goes from above the value to below the value.

Thanks for the reply.
To be more understandable.
I want to trigger a switch when the sun goes down elevation trigger (for example the lighting should turn on, after 1 hour the sun goes down dusk. The lighting should be on 4 hours. Then the lighting should turn off.) It should be done every day, should follow, elevation sun for each day.
Hope you understand what I mean.
Thanks

All ingredients to achieve that goal are here.
If itā€™s always at 1hr after sunset, iā€™d use the earlier mentioned Sun trigger and set the offset.
If you want it to turn of the light 4hrs later, you could:

  1. set a trigger at sunset with the offset 5:00
  2. set a trigger at light on for 4 hours (less reliable in case you reboot or edit automations in the meantime)
  3. set a time or datatime helper at the timestamp: now().seconds + 14400 and use that as a trigger.
1 Like

Thatā€™s why I kept it safe by saying ā€˜can imagineā€™ :grinning:
Thanks for the clarification

Thanks again Recte,
for you itā€™s easy, but for me itā€™s hard to understand, Am a little old man 65 and itā€™s actually complicated, could you help me with this, if you have the time.
Grateful for that.
This is my automation now.

- alias: 'Turn ON Hus lights on when the sun gets dim'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: -4.00
  action:
    service: switch.turn_on
    entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5 

- alias: 'Turn Off  Hus Light at dusk'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    above: -8.00
  action:
    service: switch.turn_off
    entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5
    


Trigger when:

  • One hour after sunset (turn on the switch).
  • Four hours after light has been on (turn off the switch).

Like this:

- alias: 'Automatic Hus lights'
  trigger:
    - platform: sun
      event: sunset
      offset: '01:00:00'
    - platform: state
      entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5
      to: 'on'
      for: '04:00:00'
  action:
    choose:
      - conditions: "{{ trigger.platform == 'sun' }}"
        sequence:
          service: switch.turn_on
          entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5
      - conditions: "{{ trigger.platform == 'state' and is_state('sun.sun', 'below_horizon') }}"
        sequence:
          service: switch.turn_off
          entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5

Alternately, if you prefer to trigger it based on the sunā€™s elevation (adjust the elevation to your preference):

- alias: 'Automatic Hus lights'
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      below: -4.00
    - platform: state
      entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5
      to: 'on'
      for: '04:00:00'
  action:
    choose:
      - conditions: "{{ trigger.platform == 'numeric_state' }}"
        sequence:
          service: switch.turn_on
          entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5
      - conditions: "{{ trigger.platform == 'state' and is_state('sun.sun', 'below_horizon') }}"
        sequence:
          service: switch.turn_off
          entity_id: switch.telldus_tzwp_102_plug_in_switch_switch_5

Thanks for the help. Iā€™ll try it.
Very kind of you, to help me with this.
Thanks

Hi @chris4,
Where you able to solve it?

If itā€™s complicated, the best thing to do is use the GUI where possible.
Below you see the trigger for lights on.


If you like you can add extra conditions and of course you need to add the actions at the actions section of your automation.

Below you will find the trigger for lights off.


If you like you can add extra conditions and of course you need to add the actions at the actions section of your automation.

Thanks Recte,
after several attempts I found a similar description for another automation, and it went well with the GUI, but once again thanks for the answer.
//Christian

1 Like