Is it possible to know when physical button is pressed?

Hi,

Does anyone know if it’s possible to determine, within a script or automation, if a physical button on a light or switch was pressed? I am trying to find a way to differentiate between a physical button press versus the associated HA light, switch, fan entity being switched on/off from HA. I would like to perform different actions based on physical switch, timer, motion, etc.

My devices are wifi Sonoff lights & switches running Tasmota & are configured as manual MQTT entities.

Maybe the answer comes down to whether or not the Tasmota firmware can be configured to send an MQTT payload when the relay is activated internally. Or is there another way, or any way?

Cheers
Nick

It’ll be integration, software and device dependent

I have been looking for that a long time for my Tasmota switches, but never found a solution.

Not sure how tasmota works but in esphome the switch is just a binary sensor. In esphome you can set automations to do what you want with it. it dosent necessarily need to control the light. It is also exposed in HA so you can use it there as well.

This is a little tentative, but look for posts about event context. Depending on your device and the integration there might be additional info to use and infer this. E.g. an event trigger has a context (you don’t have to use a state trigger — which doesn’t have a context — to detect state changes), and that may contain e.g. the user ID if someone pressed a button from the UI (I’ve seen this), or maybe you can detect that another automation triggered the state change (I’m not sure about this but might not be relevant).

1 Like

Hi Pieter,
Thanks for those suggestions. They do make sense to me so i’ll send some time today investingating those possibilities.
Cheers
Nick

You’re welcome, @nickh66.

Here is a more complete example using the state_changed event. Replace light.smart_bulb_3 with some light entity you have.

- alias: "Test"
  initial_state: true
  trigger:
    - platform: event
      event_type: "state_changed"
      event_data:
        entity_id: light.smart_bulb_3
  action:
    - service: persistent_notification.create
      data:
        title: Test
        message: >-
          {% set user_id = trigger.event.context.user_id %}
          {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list | first or "nobody" %}
          {% set first_name = "System" if triggered_by == "system" else state_attr(triggered_by.entity_id, "friendly_name").split()[0] %}
          {{ first_name }} switched {{ trigger.event.data.new_state.state }} {{ trigger.event.data.entity_id }}.

Output:

Relevant documentation:

You can subscribe to events for testing using the developer tools:

Resulting in, for example:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "light.smart_bulb_3",
        "old_state": {
            "entity_id": "light.smart_bulb_3",
            "state": "on",
            "attributes": {
                "min_mireds": 111,
                "max_mireds": 400,
                "supported_color_modes": [
                    "color_temp",
                    "hs"
                ],
                "color_mode": "hs",
                "brightness": 64,
                "hs_color": [
                    0,
                    0
                ],
                "rgb_color": [
                    255,
                    255,
                    255
                ],
                "xy_color": [
                    0.323,
                    0.329
                ],
                "current_power_w": 0,
                "daily_energy_kwh": 0.001,
                "monthly_energy_kwh": 0.006,
                "friendly_name": "Dining Room Light",
                "supported_features": 19,
                "icon": "mdi:dome-light"
            },
            "last_changed": "2021-08-08T15:38:29.325921+00:00",
            "last_updated": "2021-08-08T15:38:29.325921+00:00",
            "context": {
                "id": "redacted",
                "parent_id": null,
                "user_id": "redacted"
            }
        },
        "new_state": {
            "entity_id": "light.smart_bulb_3",
            "state": "off",
            "attributes": {
                "min_mireds": 111,
                "max_mireds": 400,
                "supported_color_modes": [
                    "color_temp",
                    "hs"
                ],
                "current_power_w": 0,
                "daily_energy_kwh": 0.001,
                "monthly_energy_kwh": 0.006,
                "friendly_name": "Dining Room Light",
                "supported_features": 19,
                "icon": "mdi:dome-light"
            },
            "last_changed": "2021-08-08T15:38:51.546905+00:00",
            "last_updated": "2021-08-08T15:38:51.546905+00:00",
            "context": {
                "id": "redacted",
                "parent_id": null,
                "user_id": "redacted"
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-08-08T15:38:51.546905+00:00",
    "context": {
        "id": "redacted",
        "parent_id": null,
        "user_id": "redacted"
    }
}
3 Likes

That’s awesome @parautenbach love your work.

I too did some anaylsis over the weekend to understand this a little more. Built some simple automations & scripts & otherwise did some testing to see what the state_changed events would look like in different circumstances. I came up with this simple table to at least show the various combinations of id’s that would or would not be recorded.
image
Cheers
Nick

8 Likes

I put in a feature request here to add more information to context. I think we could make a lot smarter automations with a bit more information! Add more information to "context" objects

Hi there, thank you for this, unfortunately I can’t seem to get it to work, any ideas why please?

alias: _lght state 2
description: ""
triggers:
  - trigger: state
    entity_id:
      - light.texas_instruments_coordinator_sitting_room_lamps
conditions: []
actions:
  - action: persistent_notification.create
    data:
      message: >-
        {% set user_id = trigger.event.context.user_id %}  {% set triggered_by =
        (states.person | selectattr('attributes.user_id','==', user_id)) | list
        | first or "nobody" %} {% set first_name = "System" if triggered_by ==
        "system" else state_attr(triggered_by.entity_id,
        "friendly_name").split()[0]%} {{ first_name }} switched {{
        trigger.event.data.new_state.state }} {{ trigger.event.data.entity_id
        }}.
      title: Test
mode: single

What isn’t working?

Sorry that wasn’t very helpful of me was it?

The automation does trigger when I turn on or off ‘light.texas_instruments_coordinator_sitting_room_lamps’ but no persistent notification is created.

I’ve successfully created the following code with ChatGPT to achieve exactly what I needed. Now, if the sitting room lights are turned on using Alexa, an automation will automatically adjust them to 100% brightness. This is particularly useful as the lights often turn on at 0% or a low brightness following other automations, so this ensures they consistently reach full brightness

alias: Increase Brightness for Alexa Trigger
mode: single
triggers:
  - entity_id: light.texas_instruments_coordinator_sitting_room_lamps
    to: "on"
    trigger: state
conditions:
  - condition: template
    value_template: "{{ trigger.to_state.context.user_id is not none }}"
actions:
  - data:
      brightness_pct: 100
      transition: 1
    action: light.turn_on
    target:
      entity_id: light.texas_instruments_coordinator_sitting_room_lamps