[solved] Automation w. multiple conditions (timings) under action (based on sun elevation)

Been reading and trying for hours but cannot get it to work.
I want a light to switch immediately off or delayed, depending on the sun elevation.
The media player should pause with the same delay in both conditions.
The angle of the elevation might change since I don’t know yet what is convenient.
Thanks for helping!

This is what I have but seems to be wrong:

  action:
    - condition: or
      conditions:
        - condition: template
          value_template: "{{  state_attr('sun.sun', 'elevation') >= 1 }}"
          - type: turn_off
            device_id: eadabf8dc24fa2008489a81c02a73608
            entity_id: light.l1
        - condition: template
          value_template: "{{  state_attr('sun.sun', 'elevation') <= 1 }}"
          - delay: 00:00:10
          - type: turn_off
            device_id: eadabf8dc24fa2008489a81c02a73608
            entity_id: light.l1
            domain: light
...
...
          - delay: 00:01:00
          - service: media_player.media_pause
            data: {}
            entity_id: media_player.living

I could be mistaken, but don’t you have to declare your attributes to be integers? If so then your >=1 wouldn’t work the way you think if it comes over as a string. Again, this might not be the issue, but I’ve run into situations where I had similar failures because I didn’t declare the attribute as a number (int or float) before comparing them:

"{{  (state_attr('sun.sun', 'elevation') | int) >= 1 }}"

Hi @CO_4X4, from what I have found, this should be the angle and it seems to work in the template editor…

Like I said, I could be mistaken, just sharing a similar experience/solution :slight_smile:.

Highly appreciated! :+1:

action:
  - delay:
      seconds: "{{ 0 if state_attr('sun.sun', 'elevation') >= 1 else 10 }}"
  - service: light.turn_off
    entity_id: light.l1
  - service: media_player.media_pause
    entity_id: media_player.living
1 Like

THANK YOU @anon43302295; you have whispered it right! :+1:

1 Like

It’s my forte :wink:

Hi @anon43302295, do you know/can you tell me why it happens that in some cases that delay-code gets replaced by “[object Object]” and the automation doesn’t work anymore?

Because you’re using the UI and it’s not good enough yet for dealing with anything complicated.

Automations with complex code should be put in a different file/package and kept well away from the UI editor that just wants to mangle them up.

1 Like