Trying to fine-tune automation trigger

Having problem with this automation:

- id: '1505977024304'
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  trigger:
  - below: '25'
    entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.elevation }}'
  - platform: time
    at: '16:00:00'
  condition:
  - after: '16:00:00'
    before: '22:00:00'
    condition: time
  - condition: template
    value_template: '{{states.sun.sun.attributes.elevation < 25}}'
  action:
  - service: light.turn_on
    data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100

The issue is that sometimes when I am away from home, I turn my automation off. Then on the day after the trigger has passed, ie it’s after 4PM and the sun is lower than 25° I will turn the automation back on again.

Then invariable, the light turns ON immediately and I don’t understand why it is being triggered.

I suspect it’s because it’s after 4 as the numeric state hasn’t changed below 25° but I don’t know. So is there any way I can alter the triggers so it won’t go on if say I turn the automation on at 5PM?

I’d be more inclined to think it was the elevation trigger, because it is below 25°. The time triggers at 16:00 not after 16:00.

could be but that doesn’t help…

you’re sure you need the quotes here?

I don’t use the quotes in numeric states, and that works fine.
btw, I think I learned that the only template triggers allowed are in the form of platform: template :

so I am surprised this passes the config check…

It not only passes the config check it works. The issue is only that if I enable the automation after 4 it triggers it then. Otherwise it works fine.

Maybe the condition template for the elevation is not working correctly, which would explain why it triggers after 16:00.

Have you tried to convert the value to an int before comparing it?

value_template: "{{ states.sun.sun.attributes.elevation | int < 25 }}"

ok learned something again, thx for showing me! Of course this isn’t a template trigger. Should have read the paragraph sun trigger more carefully, its there…albeit in another order, which shouldn’t be of influence in your issue?

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: >
      {{ state.attributes.elevation }}
    below: 25

and maybe still better like this, to prevent the unknown state from happening?:

{{ state_attr('sun.sun','elevation') | int < 25 }}

1 Like

Absolutely, you are right. Thanks for the input. :+1:
I’ve been using the following syntax up till now, which seemed to work as well to prevent the unknown state:

{{ states('sun.sun.attributes.elevation') | int < 25 }}

but may switch to the syntax you proposed :wink:

I’m going to remove the quotes from the 25 as you’re right…

I think converting to an integer will have unexpected consequences. Eg say the sun is at 24.99 degrees… does it round up?

In any case, you all seem to be losing sight of the fact it is working except for the unwanted trigger if I enable the automation say at 5PM when the sun is below 25. I just can’t see why it is being triggered then.

(Just for further clarification, last week before we switched to summer time, the sun was below 25 about 3:55PM but it wouldn’t trigger till 4 - correct behaviour. We switched to DST on Sunday and the sun goes below 25 at 4:55PM and it is triggering at 4:55PM as it’s after 4 - correct behaviour,)

As triggers are per default ‘OR’ and not like conditions that are ‘AND’ it seems right to me if it triggers at 5PM when sun is below 25, as at least one trigger (elevation) will fire and both conditions are met.

You could use float in that case.

no, not losing sight, just repsonding to the other triggers format. :wink:
what happens if you comment out the sun trigger, to have it only trigger on the time trigger? does it still trigger at 5Pm then?

btw, if you rewrite your time conditions as follows, it is easier to check in the dev-template:

  - condition: template
    value_template: >
      {{ 16  < now().hour + now().minute/60 < 22 }}

not sure if this would be possible, but to prevent:

you would have to be able to evaluate the trigger platform not being you… maybe a

condition: template
value_template: >
  {{trigger.platform == 'time' or 
    trigger.platform == 'sun'}}

Add this condition and it shouldn’t happen:

- condition: state
  entity_id: automation.<whatever_the_name_of_this_is>
  state: 'on'
  for:
    minutes: 5

If you just turn it on, it should’t trigger the automation.

EDIT: I should clarify, this will only work if the sun elevation is the trigger that is causing this to fire when you turn it on. (which I agree with @tom_l, I think this is the trigger). FYI this also may keep firing every time the sun elevation is below 25.

EDIT 2: You could alter the conditional value_template that you have as well. Just make it look at the from_state and verify that it was above 25. Then this will only run at the transition, the 4 pm trigger will likely never cause this to work as you’ll probably never catch the transition at 4 pm.

You’re kind of in a catch 22.

- id: '1505977024304'
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  trigger:
  - entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.elevation }}'
    below: 25
  - platform: time
    at: '16:00:00'
  condition:
  - after: '16:00:00'
    before: '22:00:00'
    condition: time
  - condition: template
    value_template: "{{ trigger.from_state.attributes.elevation > 25 and trigger.to_state.attributes.elevation < 25 }}"
  action:
  - service: light.turn_on
    data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100
1 Like

Thanks everyone for the suggestions.

Petro - I don’t think that will work. In Winter I want the lights to come on at 4PM and not before and at that time, the sun may have been below 25° since 3PM so the trigger-from and trigger-to won’t be true.

I’m going to try the automation state for 5 minutes. I do like the second one though…

You’re just in a catch-22 because it will trigger anytime between 4-10pm when the sun elevation changes. So if you turn it on and it doesn’t trigger, it may trigger whenever the sun elevation changes.

I don’t think I understand what you are saying. The way I read that value template, that condition will only be true when the elevation changes from above 25 to below 25… which is OK in summer as it will always be after 4pm But in Winter, I don’t see how it can ever be triggered as that elevation change could happen at 3PM and thus the 2 conditions won’t be satisfied ever.

maybe we are talking about 2 different things…

I would not be at all surprised! haha. I’m going to try this:

- id: '1505977024304'
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  trigger:
  - below: 25
    entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.elevation }}'
  - platform: time
    at: '16:00:00'
  condition:
  - after: '16:00:00'
    before: '22:00:00'
    condition: time
  - condition: template
    value_template: '{{states.sun.sun.attributes.elevation < 25}}'
  - condition: state
    entity_id: automation.turn_lounge_light_on_elevation__25__after_4pm
    state: 'on'
    for:
      minutes: 5
  action:
  - service: light.turn_on
    data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100

Which was your first suggestion…

Edit: I wonder if that will just delay it triggering for 5 minutes?

Yeah, it will, that’s what I was trying to say

EDIT: It may just delay it until next elevation change.

I wonder if I should just delay it for 7 hours instead of 5 minutes. Then if I enable it after 3PM it won’t have the condition satisfied until 10PM and then it will fail anyway… Or maybe I should just suck it up and switch the bloody light off manually!