Automation with IF inside condition

Hello,

I have a question if is somehow possible specify IF statement inside conditions?

I would like to achieve this:

  • If occupancy sensor is ON - perform action after sunset or before sunrise.
  • If occupancy sensor is OFF - perform action after sunset or before sunrise with reservation 1 hour after sunrise.

Here is my automation:

- id: '25'
  alias: Corridor - Light
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d00029a922_occupancy
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.0x00158d00029a922_occupancy
    from: 'on'
    to: 'off'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% elif trigger.to_state.state == "off" %}
      light.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "binary_sensor.0x00158d00029a922_occupancy" %}
        light.0x7cb03eaa00ae0000_light
        {% endif %}

I would like to have 2 conditions where will be something like this:

  condition:
condition: or
conditions:
  - condition: sun
    after: sunset
  - condition: sun
    before: sunrise
condition: or
conditions:
  - condition: sun
    after: sunset
  - condition: sun
    before: sunrise
    before_offset: '+1:00:00'

But I don’t know how tell process condition IF… Else…

The automation works perfect, except if light will be turned on before sunrise and after the sunrise for a while, then light’s won’t be turned off because condition says it’s already after sunrise. I know I can do it with 2 automations but I’m looking for single automation. Could you please help me?

First, this:

    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "binary_sensor.0x00158d00029a922_occupancy" %}
        light.0x7cb03eaa00ae0000_light
        {% endif %}

can be changed to this:

    entity_id: light.0x7cb03eaa00ae0000_light

and this:

      {% elif trigger.to_state.state == "off" %}

should be changed to this:

      {% else %}

Next, I think you’re wanting this:

- id: '25'
  alias: Corridor - Light
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d00029a922_occupancy
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.0x00158d00029a922_occupancy
    from: 'on'
    to: 'off'
  condition:
    condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.0x00158d00029a922_occupancy
        state: 'on'
      - condition: sun
        before: sunrise
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.0x00158d00029a922_occupancy
        state: 'off'
      - condition: sun
        before: sunrise
        before_offset: '01:00:00'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}
    entity_id: light.0x7cb03eaa00ae0000_light
1 Like

Works perfectly! Thank you so much! I just learned new stuff :slight_smile:

1 Like