How to trigger an automation when entity receives ON when ON or OFF when OFF?

The Event Trigger that’s been presented to you? That’s how it works. It detects the events produced by the physical device, not the associated entity’s state change.

Multiple triggers are logically ORed and cannot occur at the exact same moment in time (they may be separated by a few microseconds but that still means it’s not happening simultaneously).

What’s been presented above will fulfill this requirement.

Okey, I guess you are right about that. My fault.

So how about saving the previous value in a variable that we compare to in the condition check.
A variable that is set each time as an action when there is a button_pressed event. I don’t know how it works in Home Assistant but maybe someone can help me here.

Have you tried what was suggested? Post the automation you created and describe the testing procedure.

My Automation look like this right now:

alias: Högtalare - Gästrum
description: Slår på högtalaren i gästrummet
trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
      state: "off"
condition:
  - condition: state
    entity_id: switch.gastrum_fjarrkontroll_knapp_3
    state: "off"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.gastrum_hogtalare
mode: single

I test this by pressing:
ON → Nothing happens
ON → Nothing happens
OFF → Speaker turns on
OFF → Speaker turns on
ON → Nothing happens

How about creating a helper such as an input_boolean and always set it to the current state of the wall switch as the action after doing a conditional action that only runs if the input_boolean has the same state as the wall switch?

I prefer not to add an input_boolean. But maybe that is the way to solve the problem?

Seems like it’s doing what you asked for:

turn on the switch for the speaker in the room by pressing the button to turn off the ceiling light when the ceiling light is off.

It’s turning on the speaker switch when you press the button to turn off the light when the light is off.

If it’s not doing what you want, describe the test you performed that failed to work as expected.

I want it to work like this:

ON → Turn on speaker if previous state on wall switch was ON. Else OFF
ON → Turn on speaker
OFF → Nothing
OFF → Turn on speaker
ON → Nothing

The wall switch directly control the ceiling lamp in the room over 433Mhz and that is why I have this special requirement. I want to turn on the speaker without affecting the state of the ceiling lamp but using same button.
The speaker is then switched off after 2 hours automatically.

This topic now contains 30 posts. In which one of your previous posts did you mention this requirement?

I thought I did in my first post but I can see now that I didn’t. I just had it in my head. I’m sorry it took 30 posts to get all the correct information about what I’m trying to do.
I will update my first post with that information to make this thread clearer.

But now that I think you understand what I’m trying to do. Do you have any suggestions?

I’m sorry too because I have no free time left for this topic. Good luck.

1 Like

Got it working the way I want with the automation below.

alias: Högtalare - Gästrum
description: Slår på högtalaren i gästrummet i 1 sekunder
trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ trigger.event.data.state ==
          states('input_boolean.gastrum_fjarrkontroll_knapp_3_previous_state')
          }}
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.gastrum_hogtalare
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.gastrum_hogtalare
  - service: input_boolean.turn_{{trigger.event.data.state}}
    data: {}
    target:
      entity_id: input_boolean.gastrum_fjarrkontroll_knapp_3_previous_state
mode: single

Is it possible to do this without input_boolean?
I ask this because my next step is to convert this to a blueprint to be able to use this code in multiple rooms with the same type of setup.

You can try this

Instead of directly determining the previous state, we should just be able to accurately assume the current state by evaluating how long it has been in the current state. In this example, if it has been that state for at least 4 seconds (you can fine tune the amount of seconds as needed, but I think 4 seconds should be safe. This value just effects how quickly you can sequentially turn on/off the switch and then turn on the music.)

This should perform the following:

  • When button press “on” occurs && swtich state has been “on” for at least 4 seconds → action passes
  • When button press “on” occurs && swtich state has just changed to “on” (e.g. has NOT been “on” for at least 4 seconds) → action fails/automation ends
  • When button press “off” occurs && swtich state has been “off” for at least 4 seconds → action passes
  • When button press “off” occurs && swtich state has just changed to “off” (e.g. has NOT been “off” for at least 4 seconds) → action fails/automation ends
description: ""
mode: single
trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
      state: "off"
    id: BUTTONOFF
    alias: When Button is turned off
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
      state: "on"
    id: BUTTONON
    alias: When button is turned on
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - BUTTONOFF
          - condition: state
            entity_id: switch.gastrum_fjarrkontroll_knapp_3
            state: "Off"
            for:
              hours: 0
              minutes: 0
              seconds: 4
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.gastrum_hogtalare
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.gastrum_hogtalare
      - conditions:
          - condition: trigger
            id:
              - BUTTONON
          - condition: state
            entity_id: switch.gastrum_fjarrkontroll_knapp_3
            state: "On"
            for:
              hours: 0
              minutes: 0
              seconds: 4
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.gastrum_hogtalare
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.gastrum_hogtalare

@zimmra I like your solution but I can’t get it to work and I don’t know how to debug it. In the tracker I see this route:
image

Got it working some smaller changes of @zimmra solution.

description: ""
trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
      state: "off"
    id: BUTTONOFF
    alias: When Button is turned off
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_fjarrkontroll_knapp_3
      state: "on"
    id: BUTTONON
    alias: When button is turned on
condition:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - BUTTONOFF
          - condition: state
            entity_id: switch.gastrum_fjarrkontroll_knapp_3
            for:
              hours: 0
              minutes: 0
              seconds: 3
            state: "off"
      - condition: and
        conditions:
          - condition: trigger
            id:
              - BUTTONON
          - condition: state
            entity_id: switch.gastrum_fjarrkontroll_knapp_3
            for:
              hours: 0
              minutes: 0
              seconds: 3
            state: "on"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.gastrum_hogtalare
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.gastrum_hogtalare
mode: single
1 Like