HA boolean to control HomeKit device works every other time

I’m trying to do this very thing. Can i have someone PM me or help me with a dummies version of this with some samples?

by the way, if anyone is still having problems with this, I figured out a clean solution that solves the OP’s issue, using only 1 input_boolean.

The trick comes down to making 4 HomeKit automations, ALL with conditions. You can not do this natively with Apple’s Home app, but you can with a 3rd party app like “Controller for HomeKit.” If you don’t use the conditions, you end up in a double loop and I think it causes HomeKit or HA to barf. Once you add the conditionals, it works very nicely.

Here is the setup:
HomeAssistant: create input_boolean.switch and expose it to HomeKit using the native method.
“real” HomeKit accesory you want to control: mySwitch

automation1:
trigger: input_boolean.switch turns on
condition: mySwtich is off
scene: turn mySwitch on

automation 2:
trigger: input_boolean.switch turns off
condition: mySwitch is on
scene: turn mySwith off

automation 3:
trigger: mySwitch turns on
condition: input_boolean.switch is off
scene: turn input_boolean.switch on

automation 4:
trigger: mySwitch turns off
condition: input_boolean.switch is on
scene: turn input_boolean.switch off

With all of these set, you can control mySwitch with Siri, and the state will be matched on the input_boolean.switch in Home Assistant, and you can also automate with Home Assistant and it will be synced across perfectly.

I hope this helps someone Cheers!

2 Likes

We just added a feature to automatically turn off non cancel-able scripts and support for scenes. Both are exposed as switches inside the Home App, but once activated, they will automatically turn_off after one second.

Those features will be in the next HA release 0.83

That is awesome! Can’t wait! That should make this a lot easier!

Went through this, you only need the conditions for the automations that are triggered by the actual homekit device.
Figured i mention this since it saves some time if you’re doing this for 20 devices like I had to.

Bumping this thread, just because the OP’s use case isn’t exactly what I’m trying to do.

I have iDevices switches controlling various things and I want to keep them paired with the Home application, for firmware upgrades and to use the iDevices app (first, I’ve never been able to actually pair them to HA natively, secondly if I did, republishing back to Home.app would have them recognized as generic lights/switches and they won’t be seen as iDevices).

So, I’m building virtual lights with templates and publishing to Home.app, then building 4 automations in Home.app to handle the various statuses for turning them on/off either in Home.app or in HA. This works perfectly if I’ve created virtual Lights via a template. However, three of my items are fountains, so I wanted to export them into Home.app as a switch.

This is where the difficulty is, if I use a Switch template, then the switch in HA always turns itself off immediately after being triggered in the UI, because there isn’t anything to set the is_state to, it’s virtual and not dependent on anything but itself. I read about this issue elsewhere and it was suggested to use input_booleans, so I moved my virtual switches to input_booleans.

Now I have the original OP’s issue, they work great when triggered from the Home.app UI (either by turning on/off the actual switch, or the virtual input_boolean that was published). However, in the HA interface, the input_boolean only passes state to Home.app every OTHER time I switch it. So, the first time I turn it on, nothing happens. Then I turn it off, then turn it on again, and it works correctly. To turn it back off, I have to turn it off, then on, then back off again.

Thoughts on either making a switch template work (if so, what do I use in the value_template), or why the input_boolean is behaving this way?

-Rob

I re-read the entire thread, and it appears there are three options to resolve:

  1. Create a second input_boolean for “status” for each device, then create two additional automations in HA (so two input_booleans and 6 automations (4 HomeKit, 2 HA) just to get this to work)
  2. Use a different HomeKit application under iOS to assign conditions to the automations
  3. Just put them back to lights and have them work flawlessly the way the others work

I guess I’ll opt for #3, it’s just disappointing.

@petro, I’m trying to get my head around this… if the HomeKit automations appropriately set the input_boolean to on, and you’re trying to turn it off, isn’t this a non-issue? The problem we’re seeing (using your example) isn’t having to go on-off from an inappropriately represented off state, but rather with the light on and reflected as such in HA, having to go off-on-off.

FWIW I filed an issue on github.

It’s not a bug. If an input boolean is on, the state doesn’t change from on->on. So the automation don’t get fired.

I don’t have homekit, so I was purely guessing based on my experience with voice commands and input booleans. This thread exhibits the same issues I had with input_booleans.

If you can explain these home kit automations a bit more I can help. I tried to provide a switch template solution before and I’m fairly positive it will work, but I didn’t have enough info to provide a full solution.

I’m not sure how other folks have handled it, but all my automations are on the HomeKit side. Literally I just created the input booleans and exposed them to HomeKit as below. All my other HomeKit automations work fine, and the other reason that I don’t think the problem is on that side is that when the switch is flipped on the HA side, it doesn’t reflect as haven’t been flipped in HomeKit unless it’s flipped twice. Once it does reflect the desired state, the HomeKit automation turns on/off the desired entity. The sputnik_7 and 25 below represent brightness levels on a chandelier, and those booleans trigger automations that work with the same double-flip problem. To paraphrases the Anchorman, 50% of the time it works every time…

input_boolean:
  vs_sputnik_toggle:
  vs_sputnik_7:
  vs_sputnik_25:
  vs_gr_lamp:
  vs_fan:
  automations_on:
    initial: on
    icon: mdi:arrow-decision-outline

homekit:
  auto_start: true
  filter:
    include_domains:
      - input_boolean

Also I tried creating a template switch that refers to itself as per @vgibara above, and it turns itself off after about 10 seconds. Not sure how else to structure that.

It is a bug.

Not really sure how else to explain it. The input_boolean has been exported into HomeKit, so the states should mirror between HA and HomeKit. If the input_boolean on the HA side goes from ‘on’ to ‘off’ then the input_boolean on the HomeKit side should do the same. It doesn’t.

It’s not about moving from ‘on’ to ‘on’ and it’s not about the automation from the HomeKit side not flipping the ‘input_boolean’ on the HA side. It’s that the HA side isn’t passing the state of the input_boolean to HomeKit unless you cycle the input_boolean on the HA side twice, every time. It works so consistently it’s not a communication issue.

If you want a current fix, just create template lights and export those to HomeKit. They work as expected 100% of the time. It sucks that they come into HomeKit with the wrong icons, but you can change the icons on the HA side (I did, they look like fountains, just change the icon_template for the light) and frankly I don’t use the imported items in HomeKit at all (I hide them in a room called “Home Assistant”), since they’re only there for automation purposes.

I had the same problem as you with a switch.

That worked, thanks @pickerin!!

But it sounds like you guys are creating a feedback loop. I’ve read everywhere in the thread that it works on the first turn_on, but not subsequent turn_on's. This typically is an indication that a feedback loop is occurring.

@petro Not quite true, it works on EVERY OTHER turn on. And it works consistently that way. I’m not sure where the feedback loop would be happening. Especially since it works just fine with a light template (vs. an input_boolean). You’d figure that if there was a feedback loop on an input_boolean that it would happen as well on a light.

There was lots of talk about a feedback loop, but I don’t believe anyone proved anything. FWIW, I believe this is a bug in the input_boolean when exported to HomeKit (and others believe so as well and have submitted an issue on GitHub). I don’t see another explanation.

Not at all, you guys are creating automations that turn on and off the input boolean from homekit based on the state of the input_boolean.

Can you verify that an input boolean without automations in homekit has this behavior?

Just looking at the code. The following domains are treated the same, (I.E they go through the same code, so they all should have the same issue):

“automation”, “input_boolean”, “remote”, “scene”, “script”, and “switch” if you choose switch as the type. Everything else (including lights) is treated differently. So you should see the same behavior with all these other devices as well.

In fact, you should be able to turn on debug and watch HA set the states in the logs.

EDIT: There is an odd if statement in the set_state func, where it can ignore a turn_off call. You’d see this in the debug log too.

I have been fighting this for weeks! This fixed my issue and works great! Thanks for the info!