Adjust sun elevation offset using input_number

I’m still getting my head around templates and can’t yet get this to pass the config check.

I want a slider to be able to change the elevation offset.

What am I missing here?

- alias: Set dark boolean on
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: "{{ states('input_number.sunset_elevation_offset') | float }}"
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.dark
    - service: notify.prowl
      data_template:
        title: "Is it dark?"
        message: "It is!"

Trying it below throws up an ‘expected float’ error.

Screen2018-12-12_07-43-26_pm

Is what I’m attempting even possible or do I need to take an alternate approach?

  - alias: Set dark boolean on
      trigger:
        platform: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: "{{ (state.input_number.sunset_elevation_offset)  | float }}"
      action:
        - service: input_boolean.turn_on
          entity_id: input_boolean.dark
        - service: notify.prowl
          data_template:
            title: "Is it dark?"
            message: "It is!"

I’ve made some progress but I’m now at the edge of my capability.

When using the template checker I now get this result:

Screen2018-12-12_08-21-49_pm

Which is an improvement, but I still get the ‘expected float’ error.

I’d really appreciate some help.

Either of these ought to work.

below: "{{ states.input_number.sunset_elevation_offset.state | float }}"
below: "{{ states('input_number.sunset_elevation_offset') | float }}"

Also, try either one without the | float just to see if it’s necessary (I suspect it’s not, but I may be wrong).

Thank you so much for your help.

I tried both of those options, both with and without the | float and still get the exact same error.

When I run the first one through the template checker I get:

Error rendering template: UndefinedError: 'None' has no attribute 'state'

The second one checks out OK in the template checker but still gives me the same config error.

I’ve substituted the template for a value and the config is OK, so it’s definitely a template config problem.

Any other suggestions to try?

For completeness, can you post the configuration for this entity: input_number.sunset_elevation_offset

sunset_elevation_offset:
  name: Sunset Elevation Offset
  icon: mdi:timer
  min: -10.0
  max: 10.0
  step: 1.0

Thanks, so the initial value is the default one, namely zero.

These results are puzzling. The first error message is exactly what you’d expect if the input_number entity did not exist (but it does).

The second one works in the Template Editor but you say it fails in the actual automation. Weird.

Sorry, that’s all I got (for now).


EDIT
There’s an extra “)” in the first template I posted that I’ve since removed. Be sure to get rid of it.

Have you checked the states page to make sure the entity exists and what the state of the entity is?

Thanks for your time. Yes, the entity exists and has an initial value after restart of -10.0.

:+1: Yes, I caught that and changed it already.

I’ve also checked that the value changes with the number slider.

I have a feeling this has something to do with float or int but really don’t know where to look.

It would seem that what I’m attempting is not currently possible.

So, I’ve decided to use a binary sensor to achieve the same:

- platform: template
  sensors:
    lights_on_elevation:
      value_template: >-
        {{ (states.sun.sun.attributes.elevation | float) > (states.input_number.sunset_elevation_offset.state | float) }}

Thank you for all of the help.

I’ve come to the same conclusion. It seems like below: is not designed to handle templates.

It’s possible… just different. You need to evaluate your output in the value_template and assign it hard coded values. Then your below: attribute simply is in the middle of your 2 hardcoded values. Is this a work around? Yes, but it will work.

      trigger:
        platform: numeric_state
        entity_id: sun.sun
        value_template: "{{ 0 if state.attributes.elevation < states('input_number.sunset_elevation_offset') | float else 10 }}"
        below: 5

EDIT: You could have also just used a value_template trigger. Either is fine.

      trigger:
        platform: template
        value_template: "{{ state_attr('sun.sun','elevation') | float < states('input_number.sunset_elevation_offset') | float }}"

you beat me as I was templating this:

{{ 0 if state_attr('sun.sun','elevation') < 
        states('input_number.sunset_elevation_offset') | float else 10 }}

still, since I have also worked around this specific format for the below:, please let me ask if the state.attributes.elevation, or any other state in any other entity, can be called this was, if one first states the entity_id on its own? Not a very usual construction but very useful indeed!

1 Like

state.anything is only possible because it treats the entity_id: whatever as the state object. This is only doable inside numerical_state trigger and condition. It may be doable in other areas but they will be very specific, i.e. tied to an entity id during the setup.

1 Like

It was 40 years ago I used to program C and am just getting back to coding with this. I keep looking for variables to use, but of course, HA doesn’t currently support them.

They do support variables in certain context. They don’t support global variables. If you want global variables, you can always make a python script that stores states inside sensors.

That’s beyond me right now!