Trying to set state of button to off

Worked like a charm!

Thanks so much for the support, your help has been amazing.

This just worked great for me too. I can use the Button now for a single action and it seems to be fine.

Any ideas how we could expand to get double click and long click actions working?

I have an issue that makes this not work. So I have the automation that says if he button is pressed, then reset the state to off. Then I have another automation that uses the state to turn on a Light. So I press the button, the light turns on or off, then the other automation runs, sets the state back to off, then my other automation runs again and the light turns off.

I’m just trying to use this button to turn on and off some lights, feel like it’s close.

I just removed that delay from the automation and it seems to be working now.

I actually had to remove the delay from the switch back to off, and add a delay, of at least 600 ms to the automation to turn on the light to make the timing work out out. I will see if that is reliable. I would assume I would just have to increase the delay on the light toggle action more to ensure it goes off after the automation that sets the state back to off.

For what it’s worth, I have a version of the set_state script that works the same way for changing the state, but also allows you to change any attribute.
https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/8

Nice!

FWIW, here are some improvements you might want to consider:

if 'entity_id' not in data:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    data = data.copy()
    inputEntity = data.pop('entity_id')
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject:
        inputState = inputStateObject.state
        inputAttributesObject = inputStateObject.attributes.copy()
    else:
        inputState = 'unknown'
        inputAttributesObject = {}
    if 'state' in data:
        inputState = data.pop('state')
    logger.debug("===== new attrs: {}".format(data))
    inputAttributesObject.update(data)

    hass.states.set(inputEntity, inputState, inputAttributesObject)

Haven’t tested this, so adjust as necessary if you choose to use any of it.

EDIT: I tried it. Turns out data is not a normal dictionary, so you can’t call the pop method. So I added a line to turn it into a normal dictionary. Now it seems to work.

I’m not sure why the state matters. When I set up my Mijia button, the default state shown by the binary sensor entity is ‘on’, then when I press the button it changes to ‘off’ then immediately back to on. So I set up my automation to detect the ‘off’ state instead of on.

As far as double-click and long press, I had to look at my debug logs for those. It sends an event with a value that shows the number of clicks, so it was just a matter of grabbing that value for multi-click. It didn’t have a separate “long press” value, so I wrote a function that checks the time between the press and release. Anything over half a second is considered a long press.

Can you share your code? I just added these devices and only see temp, battery percent (which is some crazy high number), and a binary sensor which is always on. Thanks

1 Like

Oops, meant to reply to you. See my reply to the main thread.

I’m getting crazy battery with this device as well. Haven’t investigated what’s going on but it looks like the device is just reporting this number from the % battery remaining cluster.

The battery percentage is reported at twice the actual value. That way it can give you half-percent accuracy using integers. So if it’s giving you a value of 177, the actual percentage is 88.5%. I found that in the SmartThings documentation for the unit.

1 Like

Lame, cause I really need half percentage accuracy on my battery status :slight_smile: Looks like we will need a quirk to correct that I guess.

would also like to see your double click and long press code if you can post it

There is a new quirk being added to zha-device-handlers which will add zha_event’s for the button. I think David said it will arrive in 0.90.

https://github.com/dmulcahey/zha-device-handlers/blob/dm/update-new-zha-structure/zhaquirks/samjin/button.py.

Once this is in place, you will be able to use event triggers in your automations to pick up the single, double and long click events!

Once this is in, we can extend the quirk to fix the battery percentage as well.

2 Likes

For the battery percentage, just use a value template:

  - platform: template
    sensors:
      button_battery:
        friendly_name: "Button Battery"
        icon_template: "mdi:battery"
        value_template: "{{ states('sensor.samjin_button_0103374e_1_1') | multiply(0.5) | float(1) }}"
        unit_of_measurement: "%"
1 Like

This works, but its super lame that Samsung decided that a full battery is 200%.

So I was using this set state script quite nicely for a while but one of the recent Home Assistant updates seems to have broken something with this device…

I’m currently on 0.88.1 for reference.

A strange loop started happening where I had to disable the device entirely… If the button was used to turn my light on, it would turn off shortly after on its own, and then turn back on… And so on… On and off indefinitely until I disabled all the scripts and automations… Strange part is I didn’t change anything… And I just have one automation that turns the lamp on if it is off and the other turns it off if it is on… But these automations only trigger when the button is pressed.

Now… It appears the button state changes/cycles on its own which is what is causing this behavior.

Lastly, the REAL MAJOR issue I am having is that I can’t even look at the button state in the Home Assistant Dashboard… If I click on the button to load the current state and history visually, the Dashboard locks up…

Has anyone else had issues with this button?

I had the same issue, didnt bother to investigate because thankfully a quirk has been added to include zha_events for this device.

Here is my new trigger (created using automation editor, ignore wierd formatting). Just change your device_ieee to match your own device:

id: '1536818825531'
  alias: Nursery button - single
  trigger:
  - event_data:
      command: button_single
      device_ieee: 28:6d:97:00:01:00:b8:a9
    event_type: zha_event
    platform: event
  condition: []
  action:
  - data:
      entity_id: light.nursery_lamp
    service: light.toggle

Also available is button_double and button_hold commands.

Awesome, thanks for the example! I saw this was coming but didn’t realize it was out already. This is a great fix!