Sun elevation trigger

Is there any way to have a sun elevation trigger based on being below a input_number value?

      bs_aalight_extlights_nightslot:
        value_template: >
          {% set elevation = state_attr('sun.sun', 'elevation') | float %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {% set nightstart = states('input_number.in_aalight_flood_on_elev_offset') | float %}
          {% set nightstop = states('input_number.in_aalight_flood_off_elev_offset') | float %}
          {{ (elevation <= nightstart and not rising) or (elevation <= nightstop and rising) }}
        friendly_name: External Light Night Slot
        icon_template: "{{ 'mdi:weather-night' }}"```

What context is that, an automation?

How would it fit into an automation like this?

automation:
  alias: "Exterior Lighting on when dark outside"
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    # Can be a positive or negative number
    below: -4.0
  action:
    service: switch.turn_on
    entity_id: switch.exterior_lighting

That’s a template binary sensor, so that you can then use the state of that directly. You’d just use it in a state trigger

  trigger:
    platform: state
    entity_id: binary_sensor.bs_aalight_extlights_nightslot
    to: 'on'

Apologies, I assumed you just needed the syntax to get an input number into a comparison.
As Tinkerer says you ā€˜could’ use that as your trigger.
I do, as it concetrates processing in one template, so it’s more efficient, it also allows a different morning elevation to an evening elevation, so is more flexible too
The the ā€˜automation’ only evaluates when the sensor changes state

To use your example : -

automation:
  alias: "Exterior Lighting on when dark outside"
  trigger:
    platform: template
    entity_id: sun.sun, input_number.in_aalight_flood_on_elev_offset
    value_template: "{{ state_attr('sun.sun', 'elevation') | float <= states('input_number.in_aalight_flood_on_elev_offset') | float}}"
  action:
    service: switch.turn_on
    entity_id: switch.exterior_lighting

You need to ā€œReply Toā€ someone to get their attention, I only saw you responded ā€˜by accident’

Thanks! Appreciate it.

Throwing an error ā€œInvalid config for [automation]: [entity_id] is an invalid option for [automation]. Check: automation->trigger->0->entity_id. (See /config/configuration.yaml, line 446).
10:53:53 AM – config.py (ERROR)ā€

Here’s what I’m trying based on your example:

- alias: Coop Close Door Sunset
  trigger:
    platform: template
    entity_id: sun.sun, input_number.sunset_delay
    value_template: "{{ state_attr('sun.sun', 'elevation') | float <= states('input_number.sunset_delay') | float }}"
  action:
    - service: cover.close_cover
      data:
        entity_id: cover.coop_door

Edit: this works as a binary_sensor template, but not when an automation trigger.

Okay, I’m on my phone (now and then) so I’m doing this from memory.
Given the message then just comment out the entity_id: line (put a # at the beginning)
Or check the documentation on automation triggers.


Though taking a quick look, it looks like it’s required, check the yaml spacing, remember yaml likes spacing in pairs

No, there’s no ā€œentity_idā€ required for a template trigger.

1 Like

Thanks for pointing that out.
The moral of the story is its difficult to read documentation on a phone
:rofl:

Shows how useless I am without access to my code and vsc
:sob:

1 Like