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

Is it possible to trigger an automation when a entity receives ON when ON or OFF when OFF?

I want to add functionality to my wall switch (433Mhz switch rx via RFLink) so that I can 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.

Triggering the entity to change from OFF to OFF or null to OFF does not work.

alias: Speaker
description: Turn on speaker
trigger:
  - platform: state
    entity_id:
      - switch.gastrum_wall_switch
    to: "off"
    from: "off"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.gastrum_speaker
mode: single

Edit:
Information that I should have written in this first post to make it more clear was:
I want to turn on the speaker without affecting the state of the ceiling lamp but using same button.

under switches, enable events

  - platform: rflink
    device_defaults:
      fire_event: true
      signal_repetitions: 2

then in automations :

  trigger:
  - event_data:
      entity_id: switch.gastrum_wall_switch
    event_type: button_pressed
    platform: event

Thanks! So that’s a good start.
But how do I check the state to only trigger from ON to ON or OFF to OFF?

conditions ?

Yeah but how? I can only add the condition:

condition:
  - condition: state
    entity_id: switch.gastrum_wall_switch
    state: "on"

or

condition:
  - condition: state
    entity_id: switch.gastrum_wall_switch
    state: "off"

not FROM state TO state.

Excuse my stupid questions but Home Assistant is new to me. I am currently migrating from OpenHAB.

It doesn’t work because a State Trigger is designed to detect a change in the entity’s state value.

A switch entity can change states like this:

  • on to off
  • off to on
  • unavailable to on
  • on to unavailable
  • etc

There’s no such thing as a state change from off to off.

There can never be a state change from off to off.

So what I try to do is impossible in Home Assistant but possible in OpenHAB?

There must be some kind of workaround?

  • An entity’s state is set by its integration.
  • If an entity’s state is on and its integration reports off, the entity’s state is changed to off.
  • If an entity’s state is on and its integration reports on, nothing happens. The entity’s state remains unchanged.

There is no way to trigger by event like button_pressed and then check state before and after like @francisp suggested?

This is an Event Trigger. It triggers when it detects a button_pressed event and the event’s data includes a reference to entity_id with a value of switch.gastrum_wall_switch.

trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_wall_switch

Unless the event’s data contains more information, like the entity’s state, there’s nothing else you can get from it.

You can go to Developer Tools > Events and enter button_pressed in the ‘Listen for event’ field, then click the ‘Start listening’ button. Proceed to press your wall switch and its associated event will be displayed (as well as any other button_pressed events produced within Home Assistant).

The displayed event’s data is what is accessible to an Event Trigger. If there’s no state information in the event’s data then it’s simply unavailable for use in an Event Trigger.


EDIT

Francisp has performed the experiment for you (see below). The result indicates state is included in the event so it’s possible to compare it to the switch entity’s current state value (see example below).

Just listened to an event, this is what I got

event_type: button_pressed
data:
  entity_id: switch.bovenventilator
  state: "off"
origin: LOCAL
time_fired: "2023-08-07T03:11:33.187771+00:00"
context:
  id: 01H76ZMZT3633S7RKX2E6SPPX9
  parent_id: null
  user_id: null

so

trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_wall_switch
      state: "off"
condition:
  - condition: state
    entity_id: switch.gastrum_wall_switch
    state: "off"

My 2cts, I have a number of RF switched that, when triggered go to ‘on’ (it receives ‘on’ only)
My automation sets the switch state to ‘off’ after each trigger

  • state ‘on’
  • do things
  • set state ‘off’

@francisp it’s a good start but this doesn’t just trigger OFF to OFF, it also triggers from ON to OFF.

IF ON to OFF you probably would have something like this:

trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_wall_switch
      state: "on"
condition:
  - condition: state
    entity_id: switch.gastrum_wall_switch
    state: "off"

I can’t listen to events on your installation.

Play with the options, and see what works and what doesn’t.

@Ankan

If, as Francis has presented, the state from the event represents the state being requested, then you should be able to use something like the following to cover both on and off.

trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_wall_switch
condition:
  - "{{ trigger.event.data.state == states('switch.gastrum_wall_switch') }}"
action:
  - service: switch.turn_{{ 'on' if trigger.event.data.state == 'off' else 'off' }}
    data: {}
    target:
      entity_id: switch.gastrum_speaker
mode: single

This code triggers on both ON to OFF and OFF to OFF. I just want it to trigger on OFF to OFF.

trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.gastrum_wall_switch
      state: "off"
condition:
  - condition: state
    entity_id: switch.gastrum_wall_switch
    state: "off"

What I mean is that it doesn’t matter if current state is ON or OFF for switch.gastrum_wall_switch. It will trigger when I press the OFF button regardless.

that is why you have a condition, to stop any action if the condition is not true. This is one of the basic of HA automations.

Let me try to explain my problem more clearly.

When switch.gastrum_wall_switch.state == OFF and I press OFF on that wall switch I want to trigger the action.
I don’t want to trigger the action when switch.gastrum_wall_switch.state == ON and I press OFF on that wall switch.

Your code doesn’t care about the previous state of the wall switch. It just checks if I press OFF and then triggers the action.

Your code is a good start because the state_changed action only fires on change and your triggers regardless. But the problem is that I want to trigger when there is a button press but not a state change. Are you with me?

Maybe I can have a state_changed trigger and a button_pressed trigger and only run the action if only the button_pressed trigger and not the other one?