Help with trigger/condition automation

Hi, I have this automation trigger

trigger:
  - platform: state
    entity_id: sun.sun
    attribute: elevation
  - platform: state
    entity_id: !input lightgroup
    from: 'on'
    to: 'off'

That currently have this condition

condition:
  - condition: template
    value_template: "{{ is_state(haspsensor, 'ON') }}"
  - condition: state
    entity_id: !input lightgroup
    state: 'off'
  - condition: template
    value_template: "{{ state_attr('sun.sun', 'elevation') | round(default=90) <= elevationday }}"
  - condition: template
    value_template: "{{ state_attr('sun.sun', 'elevation') | round(default=-90) >= elevationnight }}"

Except I would want the last condition to be ignored if the lightgroup went from ‘on’ to ‘off’ and not only if it’s just currently ‘off’.

So the conditions that I need are (needs all to be true):

  • haspsensor is on
  • lightgroup is off
  • sun’s elevation is less than or equal to elevationday
  • sun’s elevation is more than or equal to elevationnight, unless the light group was on before it switched to off (ie, we went in because of that trigger, not the sun).

Can I check for a state transition in the condition section?

Thanks.

Probably the easiest option is to add a trigger ID and then use that as an OR clause in the final template condition:

trigger:
  - platform: state
    entity_id: sun.sun
    attribute: elevation
  - id: lightgroup
    platform: state
    entity_id: !input lightgroup #make sure to change this if you are actually creating an automation, not a blueprint.
    from: 'on'
    to: 'off'

condition:
  - condition: template
    value_template: "{{ is_state(haspsensor, 'ON') }}"
  - condition: state
    entity_id: !input lightgroup #make sure to change this if you are actually creating an automation, not a blueprint.
    state: 'off'
  - condition: template
    value_template: "{{ state_attr('sun.sun', 'elevation') | round(default=90) <= elevationday }}"
  - condition: template
    value_template: |
      {{ state_attr('sun.sun', 'elevation') | round(default=-90) >= elevationnight 
      or trigger.id == 'lightgroup' }}

Where are you creating those?

Also know that all those conditions must be true for the automation to run actions.
(perhaps you want to look at choose to change some of the conditions to apply some of the time?)

Perhaps post the whole automation instead of random pieces would help.

1 Like

Thanks. I didn’t know you could do that :slight_smile:

I posted what I thought was relevant and from the Didgeridrew’s answer, looks like it was enough. The ‘action’ part was/is irrelevant to my question.

1 Like

In case someone stumbles here looking for answer, I had to correct the code a bit. I had to define:

lightgroup: !input lightgroup

in the variables: section, replaced trigger.id for trigger.entity_id and replaced 'lightgroup' for just lightgroup (the variable I defined above). So that line now looks like this:

    value_template: |
      {{ state_attr('sun.sun', 'elevation') | round(default=-90) >= elevationnight 
      or trigger.entity_id == lightgroup }}

Thanks for directing me to the right path :slight_smile: