Help with automation template to not use numeric state with sun elevation trigger

I have an automation that fires an event when the sun’s elevation goes below five. Originally the trigger was using numeric_state as in the HA documentation example, and I have been having issues with it firing when I don’t want it to (reloading automations, etc.) So I am trying to rewrite it so it doesn’t use a numeric_state (or template) trigger. I’ve done lots of forum searching and learning and have come up with the following. But it fails a configuration check. I can’t figure out why, and would appreciate any help. Thanks!

Definition of the sensor (works fine):

platform: template
sensors:
  current_sun_elevation:
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    friendly_name: 'Current Sun Elevation'
    unit_of_measurement: 'degrees'

Automation:

alias: Set lights on event
trigger:
  - platform: homeassistant
    event: start
  - platform: state
    entity_id: sensor.current_sun_elevation
condition:
  - condition: template
    value_template: >
      {{ trigger.from_state is not none and
          trigger.to_state is not none and
          trigger.from_state.state|float(0) >= 5.0 and
          trigger.to_state.state|float(5) < 5.0 }}
action:
  - event: lights_on_event
  - service: logbook.log
    data_template:
      name: Lights on event
      message: has been fired

Configuration check error (I have my config broken up with packages a la Frenck, so I don’t get line numbers):

Invalid config for [automation]: extra keys not allowed @ data['\xa0\xa0\xa0\xa0value_template']. Got None
required key not provided @ data['condition'][0]['value_template']. Got None. (See ?, line ?).

Looks like your editor is inserting character codes HA can’t deal with.

The other problem is that when the homeassistant start trigger fires, the trigger variable will not have from_state and to_state attributes. Unfortunately it’s not documented (:man_shrugging:) but it will have two attributes: trigger.platform will be homeassistant, and trigger.event will be start.

1 Like

Hex A0 = Non-breaking space. What’s the editor you’re using?

1 Like

Thanks so much for your help. I’m using the VSCode addon for Home Assistant. Apparently I made the rookie mistake of copying and pasting bits of code from the forum :blush: I retyped the whole thing, and now I have a different error:

Invalid config for [automation]: Unexpected value for condition: 'None'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['condition'][0]. Got None. (See ?, line ?). 

The homeassistant start trigger is there because in the past, I would have an issue if I restarted HA when the transition from above to below 5 happened, and then the lights wouldn’t turn on. (You’d be surprised how many times that has happened to me!) How can I deal with that situation, then?

  - condition: template
    value_template: >
      {{ trigger.platform == 'homeassitant' and
         states('sensor.current_sun_elevation')|float(5) < 5 or
         trigger.platform == 'state' and
         trigger.from_state is not none and
         trigger.to_state is not none and
         trigger.from_state.state|float(0) >= 5.0 and
         trigger.to_state.state|float(5) < 5.0 }}

This should also work:

  - condition: template
    value_template: >
      {{ trigger.platform == 'homeassitant' and
         states('sensor.current_sun_elevation')|float(5) < 5 or
         trigger.platform == 'state' and
         trigger.from_state.state|default(0)|float(0) >= 5.0 and
         trigger.to_state.state|default(5)|float(5) < 5.0 }}
1 Like

I’m learning a lot about templating in the last couple of days, but still a novice…my understanding by reading your code is that it would evaluate as (something and something) or (something and something and something) - correct? I will try these out. The documentation indicates we should be watching for undefined variables. Your second choice doesn’t include that (I don’t think). Is one of your two options to be preferred?

Phil, thank you for being so patient and so willing to help so many of us with our templating questions. Also, thank you for all your work with the new automation options in 0.113 - I am having a great time making my automations and scripts more understandable and maintainable.

edited to add: I’ve tried both of those, and I am still getting this error when I run the configuration check:

Invalid config for [automation]: Unexpected value for condition: 'None'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['condition'][0]. Got None. (See ?, line ?).

They both do. The first specifically tests for variables that are None so as to avoid referencing an attribute that would therefore not exist. The latter uses the default filter which checks to see if its “input” is defined. If it is it passes the value through. If not, then it “outputs” the default value (i.e., the value in parentheses.)

Don’t confuse None with not defined. E.g., if trigger.from_state is None, then trigger.from_state|default(5) would return None (because trigger.from_state is defined and has the value None), whereas trigger.from_state.state|default(5) would return 5, because trigger.from_state is None and therefore trigger.from_state.state is not defined.)

Either works. It’s more personal preference. My preference used to be the former, but lately I’m liking the latter more because it’s smaller.

You’re welcome!

It must still not be liking something about how you formatted the code. Please post what you have now.

1 Like

Thanks so much for those clarifications, that’s very helpful. I didn’t pay enough attention to the default filter on the second version at first - now I understand.

I found my issue, somewhere I lost template in condition: template. There is some extension in the VSCode addon that keeps adding in code suggestions that I then have to remove… it’s more trouble than help!

Here is the automation now, which passes config check:

alias: Set lights on event
trigger:
  - platform: homeassistant
    event: start
  - platform: state
    entity_id: sensor.current_sun_elevation
condition:
  - condition: template
    value_template: >
      {{ trigger.platform == 'homeassistant' and
         states('sensor.current_sun_elevation')|float(5) < 5 or
         trigger.platform == 'state' and
         trigger.from_state.state|default(0)|float(0) >= 5.0 and
         trigger.to_state.state|default(5)|float(5) < 5.0 }}
action:
  - event: lights_on_event
  - service: logbook.log
    data_template:
      name: Lights on event
      message: has been fired

I’ll see how it works - it should trigger in an hour or so. Thank you again for all your help.

1 Like

Worked perfectly, and I did a restart and that worked as desired, too. Now I can restart and reload automations to my heart’s content and my lights will behave properly :slight_smile: Thanks, Phil!

1 Like