Not sure how to add an If Statement to my code

switch:
  - platform: gpio
    pin: GPIO13
    id: grn_led
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: template
    name: "Repeater Green LED"
    optimistic: yes
    id: grntemp1
    turn_on_action:
    - switch.turn_on: grn_led
    turn_off_action:
    - switch.turn_off: grn_led
  - platform: template
    name: "Repeater Green Flash Fast"
    optimistic: yes
    id: grntemp_f
    turn_on_action:
    - while:
        condition:
          lambda: 'return true;'
        then:
        - switch.turn_on: grn_led
        - delay: 200ms 
        - switch.turn_off: grn_led
        - delay: 200ms
    turn_off_action:
    - switch.turn_off: grn_led  

If have a Gren LED on a SONOFF. I want 2 function
1 For HA to turn the LED ON or OFF (when my burglar system is set)
2 For HA to Flash the Green LED when a gate has been left open.

The problem I have is, if the Green LED is ON (Alarm Active), but then the gate is opened LED Flashes. When the gate is close the LED remains off. As my flash template last action is to turn the LED off.

I feel I need an If Statement in the Flash template turn_off_action

Not sure to to handle this.

Regards Dave

Have now worked out how to do it, thought I would post to help others

  - platform: template
    name: "Repeater Green Solid"
    optimistic: yes
    id: grntemp1
    turn_on_action:
    - switch.turn_on: grn_led
    turn_off_action:
    - switch.turn_off: grn_led


  - platform: template
    name: "Repeater Green Flash Fast"
    optimistic: yes
    id: grntemp_f
    turn_on_action:
    - while:
        condition:
          lambda: 'return true;'
        then:
        - switch.turn_on: grn_led
        - delay: 200ms 
        - switch.turn_off: grn_led
        - delay: 200ms
    turn_off_action:
      - if:
          condition:
            switch.is_off: grntemp1
          then:
            - switch.turn_off: grn_led
          else:
            - switch.turn_on: grn_led   
1 Like