Sensor.elevation in automations

In my automations i am using this template to turn on lights when the sun elevation is at a certain degree:

value_template: '{{ state_attr("sun.sun", "elevation") < -6 }}'

Actually i am using the attributes of the sun device, but now i’d like to use the sun2 custom component which creates a sensor called sensor.elevation.

How to change that value template to use the state of this sensor ?
Thanks in advance… always learning with templates and always having headaches… :joy: :pray:

value_template: '{{ states("sensor.elevation")|float < -6 }}'

Or wait until I finish implementing the sun2 binary_sensor. You’ll be able to define a binary sensor like this:

binary_sensor:
  - platform: sun2
    monitored_conditions:
      - elevation:
          above: -6
          name: xyz

Then:

value_template: "{{ is_state('binary_sensor.xyz', 'off') }}"

Hmm, maybe I should allow a below option as well…

3 Likes

Thanks… for the moment i’ll use the sensor and then when all is ok i’ll try to use the binary_sensor that seems more reliable…

Another little problem.
I have this template in my automation {{states.sensor.sunrise.state}} that gives me as result 2019-11-23T07:02:30+01:00. How to delete the 2019-11-23T and +01:00 parts of the result to have only the hour of the sunrise?

Use the today attribute instead, since it’s a Python datetime and it’s easy to get the hour:

{{ state_attr('sensor.sunrise', 'today').hour }}

Or maybe, based on the question, you want the time???

{{ state_attr('sensor.sunrise', 'today').time() }}
1 Like

Ok, i’ll chose what is better for my automation… Thanks again !!!
One question: I delete from custom_components folder the sun component, but i always get in the logfile

2019-11-23 12:40:40 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for sun which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.

Is this referred to the sun2 component?

Nope, that message would be:

You are using a custom integration for sun2 which has not been tested by Home Assistant. ...

Unless you copied the sun2 files into a folder named config/custom_components/sun and then did:

sensor:
  - platform: sun
    ...

which is possible, but I doubt you did that.

Are you sure you don’t have anything named sun or sun.py in config/custom_components?

No, i don’t copied the sun2 files, in custom_components i have only the sun2 folder, but i don’t get that in the logfile, in sensors i have:

- platform: sun2
  monitored_conditions:
    - sunrise
    - sunset
    - solar_midnight
    - astronomical_dawn
    - nautical_dawn
    - dawn
    - solar_noon
    - dusk
    - nautical_dusk
    - astronomical_dusk
    - daylight
    - civil_daylight
    - nautical_daylight
    - astronomical_daylight
    - night
    - civil_night
    - nautical_night
    - astronomical_night
    - max_elevation
    - elevation

Why there is no sun2 entry in the logfile?

I really don’t know. Did you actually delete the old sun folder, or did you move it somewhere? I’ve seen people have issues when they move a custom integration from config/custom_components to config (just in case…) That will definitely cause problems.

I renamed the folder to “bak_sun_bak” but now i deleted. Will restart and see what happens.

Ah, that might explain it. I seem to recall running into something just like that before. Couldn’t figure out how HA could find it when the folder name was changed, but somehow it was still finding the cache files in the __pycache__ subfolder. Had to completely delete it to fix the problem.

Ok, let you know…

I am trying to use this template

 {{ state_attr('sensor.sunrise', 'today').time() }}

Inside the hadashboard configuration, but i really don’t know how to do this.
Could it be possible to create a sensor with those attributes so i can then use in HADashboard?

I don’t know anything about HADashboard, but yes, you can certainly create a Template Sensor. E.g.,

sensor:
  - platform: template
    sensors:
      sunrise_today_time:
        value_template: "{{ state_attr('sensor.sunrise', 'today').time() }}"
1 Like