#HA (MQTT) button - Runs HA Automation on "online/offline" or discovery/remove.

1 messages ยท Page 1 of 1 (latest)

fallen saffron
#

Hey guys, hopefully a facepalm here, but I've added MQTT Button exposed to HA from YAML, it sends a timestamp when the state changes, great!

Except, it triggers the assigned HA Automation when it becomes unavailable, online or offline using the availability topic.
https://www.home-assistant.io/integrations/button/#device-class:~:text=effectively captures when the button is pressed .

My intended outcome would be the button not to run on MQTT Discovery & only run when it is pressed by user or something else other than just being discovered, cheers.

Home Assistant

Instructions on how to set up your button with Home Assistant.

fallen saffron
#

I still fail to see why anything is being turned on/off from an /availability topic.

#
  • If the device is added, then Automation doesn't run.

  • If device is removed, then Automation runs.

  • If the device is added, then removed the Automation runs.

  • If the device is using an /activity topic, then turned on-line after discovery, Automation runs.

  • If device is toggled online/offline then Automation runs.

It's all on the /config topic, not the /command topic.

fallen saffron
#

๐Ÿคท

fallen saffron
#

Hmm, would sharing an example flow help here?

#

Like this seems like a bug now.

native trellis
#

Can you share your MQTT button config YAML?

fallen saffron
#

I'm changing this over to a YAML entry now.

#

Was as NR - MQTT Discovery entity, Just testing the YAML so I can update the issue, cheers ๐Ÿ™‚

native trellis
#

Oh I missed that

#

You can share the MQTT discovery payload if you want

fallen saffron
#

I did need to test this just in case was some odd NR behaviour.
OK same problem persists with YAML, cool least we cleared that up.

#

ok YAML

# Example configuration.yaml entry BUTTON
mqtt:

  - button:
      unique_id: MW3D-B-941
      name: "B-941"
      command_topic: "homeassistant/button/B-941/command"
      availability:
        - topic: "homeassistant/button/B-941/available"
      payload_press: "Pressed or whatever this doesn seem to matter"
      enabled_by_default: true
      qos: 0
      retain: false
      device:
        identifiers: "B-941"
        name: "B-941"
        manufacturer: "DeSwaggy"
        model: "YAML Button Version for HA"
        sw_version: "4.0.6"


# Example configuration.yaml entry LIGHT

  - light:
      unique_id: MW3D-L-981
      schema: json
      name: L-981
      command_topic: homeassistant/light/L-981/command
      state_topic: homeassistant/light/L-981/state
      supported_color_modes:
        - onoff
      brightness: false
      brightness_scale: 255
      effect: true
      effect_list:
        - STOP Effect
        - Blink
      flash_time_short: 1
      flash_time_long: 5
      optimistic: true
      enabled_by_default: true
      device:
        identifiers: L-981
        name: L-981
        manufacturer: DeSwaggy
        model: "YAML Light Version for HA"
        sw_version: 0.2.8
#

They are just dummy examples I don't know if you need to add them to test my issue.

#

OK so the test light won't return a state as its just a dummy, so I turn optimist=true so can see a feedback from the button.

#

so the button has an availability: - topic: "homeassistant/button/B-941/available".

Create a HA Automation, so the Button toggles the light. no other conditions.

#

If you send an MQTT payload to the button to turn it "online" the light will toggle.

#

^^^ Thats the issue.

fallen saffron
#

So add MQTT button to YAML, then on Activity Topic, turn button online - light will come on.

#

It a far bigger issue if you remove the button device via MQTT discovery, the light toggles, if you online or offline the button, the light toggles.

fallen saffron
#

HA (MQTT) button - Runs HA Automation on "online/offline" or discovery/remove.

fallen saffron
#

๐Ÿคฆ Same issue, if the device is a "binary_sensor", if online/offline in the availability topic, runs the HA Automation.

#

Why is the Automation running from the availability topic, the automation should be turned on from the command topic, this seems like a bug.

native trellis
#

Can you share the automation YAML? I can't look at this now but I can in about 8 hours unless someone beats me to it of course

fallen saffron
#
domain: button
entity_id: d392d5ecfbc68a2966473ec453e03b9e
type: pressed
trigger: device```
#

Then do: Light 'Toggle' on L-981 L-981

metadata: {}
data: {}
target:
  entity_id: light.l_981_l_981```
#

Its as potato as I can make it in my mind, just why it's firing off availability topic ("online", "offline", "add" device, "remove" device) has my mind blown.
I would expect it to run the Automation only on the "command topic".

fallen saffron
#

OK, Made some progress here..
So:

  • if the Automation is action: light.toggle then this issue of turning the light on/off on available topic "online, offline, add or remove device occurs.

  • if the Automation is action: light.turn_on then all works as expected.

I think it's a bug.

native trellis
#

i haven't done too much with button entities in HA (and they're somewhat new), but in this particular case if you don't need an actual entity, I think you're better off ditching the button entirely and using an MQTT trigger in the automation

fallen saffron
fallen saffron
# native trellis i haven't done too much with button entities in HA (and they're somewhat new), b...

I've tried it with trigger conditions, but same issue.

The "button" represents a physical wall button, so if this button is physically pressed it it runs to a call service and turns say a light on, if the light is off it turns it on, works fine in the backend using NR, however I've now added the buttons to be exposed on the frontend & would like to move the automation rules to HA in away from NR so others can make rules without messing around in NR for ease of use, but this is no good if anytime the device is updated on a config topic it runs the automation.

The frontend exposed button to the UI if pressed should & does the same action as if it was push on the wall, it works well.

native trellis
#

Just confirming, you set up something like this in an automation and you're still having issues?

trigger:
- platform: mqtt
  topic: homeassistant/button/B-941/command
  payload: 'press' # whatever payload you're sending with a button press

That being said, I would recommend not using homeassistant as the prefix for the command topic since that's what Home Assistant uses by default for MQTT discovery

native trellis
#

In fact, I bet that's the source of the issues you're experiencing

hallow tangle
#

using a device trigger means it will trigger to/from any state change and unavailable is a state

#

As a sidebar, you're using the wrong type of entity

#

you should be using event

#

Buttons are for when you press something in the UI and it actions something outside HA

#

buttons are not meant to update from physical presses, that goes against their original design intent.

native trellis
#

that actually makes way more sense about the intent of buttons

fallen saffron
fallen saffron
#

Reading ๐Ÿ˜˜

fallen saffron
native trellis
#

It does, yeah. I'm wondering if that's causing some weirdness though, but I'm also having trouble keeping everything in this thread straight lol

#

I do still think you should ditch the button device in HA entirely and switch to an MQTT trigger with the command topic

fallen saffron
#

I think petro also raises a good point if unavailable is a state, thou seems strange to fire of discovery add/remove but I guess that's creating the unavailable state. I've put a fair bit of time into the buttons and was pretty happy with the UI & Wall button thingy, but I need to get over it I guess and yep try something else, I'll look into an MQTT Trigger.

fallen saffron
native trellis
#

again, I don't think MQTT discovery (or the button device by extension) is really needed at all in this scenario

#

looking back at your original post, it looks like you didn't really want MQTT discovery for it either

fallen saffron
#

Ah HA Recommends HA Discovery, idk just trying to follow the rules.

native trellis
#

for things like switches, lights, etc. for sure

fallen saffron
#

This is a light switch, well push button light switch.

#

So a button ๐Ÿ˜‚

native trellis
#

it just sends a message on press as opposed to having a state (on/off), right?

fallen saffron
#

yes

native trellis
#

yeah, MQTT trigger is definitely my recommendation then

#

or an event trigger if it didn't specifically use MQTT

#

also MQTT discovery is fine for a button, but as petro said, buttons in HA are for sending a command to something, not for receiving a state/command from something

hallow tangle
#

button entities are the hardware version of an HA script

native trellis
#

and I wish I had known that earlier because we could have resolved this whole thing on day 1 lol

hallow tangle
#

event entities are what you want to create

#

or the topic trigger, any of that works

native trellis
#

oh wow, i totally missed that those exist now

hallow tangle
#

only 2 or 3 years late ๐Ÿ˜‰

native trellis
#

2 apparently, shit

#

The Event entity was introduced in Home Assistant 2023.8.

hallow tangle
#

They do exactly what OP is trying to do, however they also have the same issue with unavailable

native trellis
#

ah

#

hence your other recommendation about not_from

hallow tangle
#

although the automation changes next month will likely make event entities even easier without needing not_from

native trellis
#

wait, changes are coming?

hallow tangle
#

Yes

native trellis
hallow tangle
#

new frameworks for integrations, essentially allowing any integration to create a custom trigger

native trellis
#

|| that was sarcasm ||

hallow tangle
#

well I'm excited regardless of your sarcasm

native trellis
#

that is pretty cool though

hallow tangle
#

it will likely reduce the need for templates in automations 10fold

native trellis
#

ooo

clever creek
# fallen saffron So a button ๐Ÿ˜‚

a button entity is intended to be pushed in Home Assistant. You don't want to push this button in Home Assistant, you want to know when the physical button was pushed. So that would be an event

native trellis
#

you're late to the party lol

hallow tangle
#

Tisk tisk @clever creek

#

Time for a beatdown