Automation based on sun angle not triggering

alias: Morning Lights On
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 1f63b4c053dd7170788116662b41d19c
    entity_id: binary_sensor.officesensor_home_security_motion_detection
    domain: binary_sensor
condition:
  - condition: state
    entity_id: sun.sun
    state: 'true'
    attribute: rising
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: '-5.0'
    below: '45.0'
action:
  - service: scene.turn_on
    target:
      entity_id: scene.lights_on
    metadata: {}
mode: single

In the morning when I walk into the office and the sun is between -5 degrees and 45 degrees, turn on the lights…

I can see this morning that the motion detector triggered at 7:22 AM which should have had sun rising and angle between -5 and 45… (NOAA online calculator says at that time sun elevation would have been 15.79 in Los Angeles)… but it didn’t trigger. Am I doing something wrong here?

I’m getting a little rusty, but my guess would be you have the wrong state for the first condition. I.e., the rising attribute is a boolean, not a string. So try removing the quotes:

  - condition: state
    entity_id: sun.sun
    state: true
    attribute: rising

Ok, will try that. This was actually created in the GUI editor, so the quotes were put there by the editor… meh. But thanks!

I think you can do it in the GUI editor. Just click on the three dots in that condition and select “Edit in YAML”. Then you can remove the quotes. But I don’t really use the GUI editor.

The GUI editor will keep “fixing” true to 'true', even when you edit other automations.

The only answer is to split the automation out into another file. For example:

automation: !include automations.yaml
automation mine: !include my_automations.yaml
1 Like

Again, I don’t use the GUI editor, so I don’t know how well this would work, but another solution could be to replace both conditions with a single template condition:

{{ is_state_attr("sun.sun", "rising", true) and -5 < state_attr("sun.sun", "elevation") < 45 }}

If the UI editor is changing a boolean to a string using an attribute (that is supposed to adopt native datatypes automatically) then this sounds like a bug that needs to be reported.

1 Like

Ok, so in fact after removing the quotes using the YAML editor in the gui, it did work this morning!

I agree with @finity that this is a bug, but not sure where to report.

There’s an issue in the frontend repository already:

However, it is stale for some reason. I don’t currently have time to read through it, but if you believe it still applies, please comment on it to prevent it from being automatically closed.

thanks, added a comment and a link to this topic.