How can I resync switches after connection loss?

I’ve made an ESPHome device that exposes among others a switch to HA.
The device has code so that should it lose connection it would turn the switch on, that works great. Now the issue is that once it reconnects it will not check the state of the switch in HA, how can I make it do it?
If the switch is off in HA I’d like the device to see it and turn itself back off.

Great! Mind showing it to us like described in How to help us help you - or How to ask a good question?

Just guessing (as bare minimum information is missing in your post) but it is probably the same switch (located “in” the esphome node) and it is just exposed to HA - so the “resync” happens correctly probably as HA shows the switch as ON ones the esphome nodes reconnects.

The same way you have the logic already with api.connected condition when it turns the switch on when connection to HA is lost :bulb:

I indeed gave a simplified version, but yes HA syncs to the esphome node’s status, which in your words is “correct”. I made my post precisely because I need it to work the other way, the esphome node should sync to HA’s current state and I’m asking how to do that.

I can’t seem to find a way to implement “get the current state from HA” to execute as a response to the “connected” event.

I think you might need to create a HA template switch or input boolean and have that as your “target state” entity (primary control switch).

Then import this into ESPHOME.

And have automations on both the ESPHome and HA side to do the interlocking / syncing.

When the api connects it will import the state of your target state switch and you can use that to trigger switching the state of your ESPHOME switch.

Yeah, sounds like that would work. Thanks for the idea!

1 Like