I have an input_boolean that is used in an ESPHome script. I’m saving that state in a global to survive a reboot. That seems to work fine.
But if the input_boolean’s state changes while the ESP is disconnected it doesn’t refresh once the API reconnects. Is there a way, perhaps with on_boot for the ESP to get an updated state from HA?
I just tried that. That’s asking the integration to immediately update the entity’s state in HA. When I run that action nothing is logged in the ESP’s logs.
But I figured out the issue. When the ESPHome reboots it must set its state of the entity as something like “unknown.” So, when the API reconnects it does send the current state of the entity to the ESP, but that doesn’t trigger on_state.
But using on_state_changed does trigger on the state change from the after-boot state of the entity in ESPHome.
Update: or setting the trigger_on_initial_state option.
Sorry, I was saying calling the HA action homeassistant.update_entity doesn’t really do anything on the ESP – it’s not forcing an update. The update happens when the API reconnects.
Your lambda does work to copy the now-refreshed state to the global. And, well, I did ask about doing it in on_boot, but the real issue was on_state didn’t fire after a reboot when the ESP’s state for that entity was unknown. Just using on_state_changed allows it to trigger after the reboot.