Automation not getting fired?

Hey all,

I have an automation that for some reason doesn’t get fired.
If I manually trigger it, it seems to work as it should though. Can anyone see what’s wrong in my trigger?

### Check Outdoor Light After Sunset ###    
- alias: '[Outdoor] Check Light'
  initial_state: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -5.0
  condition:
    condition: template
    value_template: > 
      # this counts the lights that are off and if we have any that are off, the notify will occur
      {{ states | selectattr('entity_id', 'in', state_attr('group.outdoor_lights','entity_id')) | selectattr('state','in',['off']) | list | length >= 1 }}
  action:
    - service: notify.ios
      data_template:
        message: > 
          {% set off_lights = states | selectattr('entity_id', 'in', state_attr('group.outdoor_lights','entity_id')) | selectattr('state','in',['off']) | map(attribute='name') | list %}
          {% if off_lights | length == 1 %}
            {{ off_lights[0] }} is still turned off.
          {% else %}
            {{ off_lights[:-1] | join(', ') }}{{'' if off_lights | length > 2 else ''}} and {{ off_lights[-1]}} is still off.
          {% endif %}
        data:
          push:
            category: "outdoorlight_ios_notification_sunset_trigger"

Thanks…

Is the sun component enabled, can you see it in Dev tools -> Entities?

Try to break your automation down to smaller pieces by removing the condition for the start.
Check if your condition is True when you trigger your automation manually as only action is evaluated on that occassion (place the code in Template editor).

And apparently you no longer need that initial_state: true :wink:

I tried removing the trigger and now the template editor shows the output correctly.
Also, as you stated I removed the intitial_state - just haven’t got to the cleaning part of my automations yet :slight_smile:

I know my sun component is enabled, because I use the exact same trigger for turning my lights on which works.

You mean condition (as it won’t work without trigger)?
I suggested to paste the condition template into Template editor to see if it resolves to True, otherwise the action block won’t run.

So what’s your progress? :wink:

Well, if I open up the template editor and paste in the full code it just gives me this error:

Error rendering template: UndefinedError: ‘state’ is undefined

If I then remove the trigger part:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -5.0

The it shows the output of which lamps are still turned off which is expected.
I just read through the sun component again, and the trigger is written exactly as it is in the documentation :frowning:

Well yeah, that’s not going to work. You don’t have a state.

If you are trying to check your templates, only paste the condition/action templates into the template editor. Trigger templates are iffy because you don’t have access to the objects because they only exist momentarily.

Ah okay - that makes sense.
So even though the template editor don’t like the trigger, it should still get fired right?

You need to paste this

{{ states | selectattr('entity_id', 'in', state_attr('group.outdoor_lights','entity_id')) | selectattr('state','in',['off']) | list | length >= 1 }}

And metro is correct,

does not exist, does it?

Don’t know why they put it as an example… it should be

states.sun.sun.attributes.elevation

{{ states | selectattr('entity_id', 'in', state_attr('group.outdoor_lights','entity_id')) | selectattr('state','in',['off']) | list | length >= 1 }}

… and yes the output is then True

I do use it in other automations and it works fine there though?

you can use template editor to check if your code is correct and works as expected

if you trigger it manually, it only executes action, no trigger or condition involved. pretty useless tbh unless you know what you’re doing

It does exist, during the trigger. It’s a weird one for sure.

what’s the output if you paste that value_template in Template editor?

value_template: > 
      # this counts the lights that are off and if we have any that are off, the notify will occur
      True

You said you have another automation with the same trigger, yes? If that fires then so will this one and stop trying to look at the trigger and look elsewhere, like your condition.

I believe trigger.state does, but state does not, am I wrong?

I’m talking about the trigger

Yeah, you have that wrong. It exists. State will exist during any numeric state trigger. It’s the state object.

I tried, but the problem is the rest it providing me the output I need. I get all the lamps that are turned off.
When I look at my automations the other ones are fired exactly when the elevation is below - 4.0 and when we get below - 5.0 which should trigger this automation we are talking about - nothing happens. It just states that the automation has never run.

Where does it say so? I can see only this

This is the output that I get if I remove the trigger part, which is as stated before, the same I use in other automations which fires.

Check Outdoor Light After Sunset

- alias: '[Outdoor] Check Light'
  condition:
    condition: template
    value_template: > 
      # this counts the lights that are off and if we have any that are off, the notify will occur
      True
  action:
    - service: notify.ios
      data_template:
        message: > 
          
          
            Lamp1, Lamp2, Lamp3, Lamp4, Lamp5, and Lamp6 is still turned off.
          
        data:
          push:
            category: "udelys_ios_notification_sunset_trigger"

Maybe I just need to trigger it somehow different.