Automation condition for repeated trigger

Can anyone advise:

Requirement: Automation triggers if “state” is set “true” twice in a row.
I.E. Condition checks if remote ON switch (external to HASS) has been pressed twice (I.E. On then On again, without a Off in-between).

Current thinking is below, which does not work, as condition is always true as trigger and condition monitor the same state, so the condition is always true.

- alias: Kitchen Extractor Enable Auto Off
  trigger:
    platform: state
    entity_id: switch.kitchen_extractor_fan # Wireless 433 switch, picked up by RFLink
    state: 'on'
  condition:
    # Only scrtipstart if the fan is already on
    condition: state
    entity_id: switch.kitchen_extractor_fan
    state: 'on'
  action:
    service: script.turn_on
    data:
      entity_id: script.kitchen_extractor_timer

This is not making a lot of sense to me. How do you turn on the switch switch.kitchen_extractor_fan if it is already on? The automation will not trigger because there is no state change when the switch is already on.

I updated by post to add more info, tigger is from https://home-assistant.io/components/switch.rflink/

Yes I just tested it and if the “switch.kitchen_extractor_fan” state is already “on” then the automation will not run at all.

I need to change the logic to, how about:

If “switch.kitchen_extractor_fan” == OFF and “script.kitchen_extractor_timer” ==OFF
then: switch.kitchen_extractor_fan = ON
else if “switch.kitchen_extractor_fan” == ON and “script.kitchen_extractor_timer” ==OFF
then “script.kitchen_extractor_timer” == ON

Kinda sounds like something I am doing with my mailbox alerts. I want the sensor on the mailbox to trip a notification when it is opened by the mail carrier putting mail in the box. But I don’t want a second notification when I open the mailbox to retrieve it.

So I use an input boolean to track the state like this:

Input Boolean:

mailbox_switch:
    name: Toggle Mailbox Automation
    initial: off
    icon: mdi:email

Automation for mail alert (home version; there is also one for notifications when I am away):

# Notify when Mail has arrived - Home
- alias: 'Mail Alert Home'
  trigger:
    platform: state
    entity_id: binary_sensor.mailbox_opened
    from: 'off'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.mailbox_switch
        state: 'off'
      - condition: state
        entity_id: device_tracker.rpitera_rpitera
        state: 'home'
  action:
    - service: tts.google_say
      entity_id: media_player.living_room_home
      data:
        message: 'Pardon the interruption, Robert but the mail has been delivered.'
    - service: input_boolean.turn_on
      entity_id: input_boolean.mailbox_switch

Reset automation accounting for me opening the box:

# Works with Mail Alert routines, sets flag to prevent new notification
# when retrieving the mail
- alias: 'Mail Retrieval'
  trigger:
    platform: state
    entity_id: binary_sensor.mailbox_opened
    from: 'off'
  condition:
    condition: state
    entity_id: input_boolean.mailbox_switch
    state: 'on'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.mailbox_switch
1 Like

I see your logic, but my trigger will not be triggered a second time as it stays on and does not transition to off.

Sorry, didn’t think about that. Hopefully it gives you another idea.

If you are handy with Python, you might consider using AppDaemon. You could probably create an AD app to accomplish this.

Just an idea but if pressing the on button on the remote shows up as an event on home assistant you could use that.
For example

- alias: Kitchen Extractor Enable Auto Off
  trigger:
    platform: state
    entity_id: switch.kitchen_extractor_fan # Wireless 433 switch, picked up by RFLink
    state: 'on'
  action:
    - wait_template: "{{ (button press event) }}"
      timeout: 00:00:10
    - service: script.turn_on
      data:
        entity_id: script.kitchen_extractor_timer
1 Like

Thank you, I tried your idea and was really hopeful :

- alias: Kitchen Extractor Enable Auto Off
  trigger:
    platform: state
    entity_id: switch.kitchen_extractor_fan
    state: 'on'
  action:
    - wait_template: "{{ states.switch.kitchen_extractor_fan.state == 'on' }}"
      timeout: 00:00:10
    - service: script.turn_on
      data:
        entity_id: script.kitchen_extractor_timer

However, RFLink Switch (https://home-assistant.io/components/switch.rflink) seems to create an event on a state change so two ‘on’ in a row is ignored.

I may have to give up on this route.

1 Like

Hi,
I see this tread is some months old, so meaby you already figured this out.
But I have a 433 MHz switch that do different things if it is pressed twice.

For you it will look something like this:

 - alias: "Kitchen Extractor Enable Auto Off"
   trigger:
    - platform: event
      event_type: button_pressed
      event_data: {"entity_id": "switch.kitchen_extractor_fan", "state": "on"}
   condition:
    condition: state
    entity_id: switch.kitchen_extractor_fan
    state: 'on'   
   action:
     service: script.turn_on
     data:
       entity_id: script.kitchen_extractor_timer

I use rfxcom, but think it will work with your system too.

And it is important that you have fire_event to true on your switch, if not, this will not work.

Hope this helps.

1 Like

@Zepixir Many thanks I will give that a go.