[solved with a template] Sun elevation & threshold

I’m trying to use the sun elevation as a condition for a sensor.
The sensor switches on but not at the right moments.

The goal is to switch on at about 4° and off around -5°.
I have played with the upper/lower limits and hysteresis but I cannot get it right.
Is it because of that negative value?

Could be value… or the time of day or your trigger…maybe post it so people can have a look?
Also…recently I got pointed to the Sun2 custom solution…also very nice creating various sensors (instead of using the attribs)

1 Like

A condition doesn’t switch anything on or off. It tells the system when an automation may or may not run.

1 Like

Hi guys, thanks for helping.

So I have a sensor (sensor.sun_elevation) that stores the sun’s elevation angle.
I would like to use that value with the threshold integration to create a sensor that switches on & off at 4° and -5° respectively.
Tried with different limits/hysteresis values, using only lower or upper, …

@vingerha

entity_id: sensor.sun_elevation
hysteresis: 5
lower: 7 #(also tried with other values)
position: above
sensor_value: 20.86
type: lower
upper: null
friendly_name: testsun

Thanks for that hint on the sun2 sensor but I don’t see how this can help.

@nickrout: I know that the condition on itself doesn’t do anything but if the sensor is not working as expected, the result of the automation will also not be what I wanted so I’m trying to get this right at first.

Is what I like to achieve not possible with a threshold sensor?

Why a threshold and not a regular binary sensor with a template?

template:
  - binary_sensor:
      - name: "ElevationCheckABC"
      - state: {{ state_attr('sun.sun','elevation') > -5 and state_attr('sun.sun','elevation') < 4 }}
1 Like

Because I thought that I could get it running that way and without asking for help.

Uhm … I’m not good at templating… :blush:

Thanks a lot!

Whatever values I try in the template editor, the result remains false…

Sun2 has a binary sensor you can switch on or off at a specific elevation. It is exactly what you are looking for.

1 Like

@DavidFW1960 is right. The sun2 is great for this.

Regardless, I think you need to have it as an int/float otherwise you’re comparing a string to a number.

{{ state_attr('sun.sun','elevation') | int(0) > -5 and state_attr('sun.sun','elevation')| int(0) < 4 }}

sun2 example:

binary_sensor:
  - platform: sun2
    monitored_conditions:
      - elevation:
          name: Sun Daylight
          above: 12
1 Like

The straight sun sensor works. My condition in an automation is

condition:
  - condition: template
    value_template: '{{ state_attr("sun.sun", "elevation") < 4 }}'

The result of state_attr('sun.sun','elevation') is already a number. Try it in the template editor.

2 Likes

Never said it didn’t. Just the binary sensor in sun2 is great for this

1 Like

I’m not saying you are wrong. I am just trying to help out the OP with his templates, which he says are not rendering properly. (Mind you, he hasn’t showed us them yet!)

1 Like

Guys, thanks for the tip on the sun2 sensor.
It’s in my nature, and seems more logic to me, to try to work with standard tools (dunno how to explain it in a better way) so I was under the impression that I don’t need any additional things then ‘standard HA’ to get this working.
I mean, I try to avoid adding/installing extra stuff if it’s not needed.

That doesn’t work.

@nickrout: this one switches on! :+1:

This is my template:

template:
  - binary_sensor:
      - name: "testsun"
        state: >
          {{ state_attr("sun.sun", "elevation") < 4 }}

I also tried with a threshold sensor created as a helper.

Thank you for your persistence to help me, really appreciate it!
I have posted the code in my reply to nickrout (have you not seen it?): there is nothing more yet because I wanted to get the condition/sensor right first hence the ‘testsun’ name.

Nickrout’s reply is exactly what I needed; it gives me what I need to use as a condition in my automations.

I’d like to share what I have on this subject. This also includes dusk and dawn, so not binary sensor any more. Obviously you can play with the thresholds to get a better fit to where you are.

template:
  - sensor:
      # dawn-day-dusk-night
      - name: "Period of the day"
        unique_id: period_of_the_day
        state: >
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -4.4 -%}
            night
          {%- elif -4.4 < elevation <= +3 -%}
            {{ 'dawn' if rising else 'dusk' }}
          {%- else -%}
            day
          {%- endif -%}light
        icon: >-
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -4.4 -%}
            mdi:weather-night
          {%- elif -4.4 < elevation <= +3 -%}
            mdi:weather-sunset-{{ 'up' if rising else 'down' }}
          {% else %}
            mdi:weather-sunny
          {% endif %}

The idea and codes are not mine, but here and there from the community (couldn’t recall where now). Just figured it is a good opportunity to share.

4 Likes

I did miss it and nickrout’soluion not what you asked for you asked for a range which I provided before…anyhow… you are fine that is what counts :slight_smile:

1 Like

Notice that even by providing only 1 value you also get a range.

Thank you! Very useful.
Notice that the row " {%- endif -%}light" should be changed in “{%- endif -%}” otherwise you get i.e. nightlight

That was intentional.