Can I poll the ESP from Hass to avoid relay switches being unknown after boot?

When I boot the Pi, I sometimes loose the state of some of the switches. it seems like it’s the physical GPIO relay switches that have the biggest problem there. Is there a way to force an update from the ESP to Hass so that I get the correct state of the switches into Lovelace? When they are unknown my custom toggle switches naturally do not work. Here is an example from one of the around ten ESPs in my boat:

The physical relays are shown as unknown by the lightning symbol, the template switches are shown as off and on. The “Hovedstrøm” (“Main power”) is the only physical GPIO switch showing the correct value, that’s because I had to turn it on and off again to check that it wasn’t eating power, the voltage was a bit lower then I prefer it. Turned out that the CTEK charger had entered into a power save program. Before I did that it showed the lightning symbols as well.

So what I’m looking for is something I can send on Hass startup to refresh the value of all switches. The ESP is totally aware what’s on and what’s off:

Using retain for the switches on and off does not really work, since they can be triggered by something in the script (temperature, time of day whatever) while the Pi with the MQTT broker/server is booting, which means that the MQTT message will be lost anyway.

Is there a reason why you can’t/don’t use the native api?

In cases like these I normally do a stack card with Markdown card and button cards to make a copy of the entities card with extra features, like the ability to color the text yellow if the state is unknown or unavailable or add extra symbols.

You can try using the update entity service. There is a trigger, home assistant start that will fire after ha restarts. Use started instead of start it will give the integrations time to load.

Thanks for answering, y’all!

@Mahko_Mahko Yes. I also access the same devices over MQTT with Node-RED (standalone on another Pi in the boat), SignalK (boating software) and a custom Python script. And finally, when the boat is at my dock it connects over wifi to the cabin network, and all MQTT messages are forwarded to EventGhost in the cabin. I found that using both MQTT and API was unstable, which led me to only MQTT.

@WallyR I really need them to be correct, if it’s at all possible. Maybe I could force them to send an update by using an “if → then” that on message checks the state of the switch and sends back a message that imitates on/off?

@Mikefila I tried this code and ran it manually, but unfortunately it did not update anything.

service: homeassistant.update_entity
metadata: {}
data: {}
target:
  entity_id: switch.hekk_hovedstrom

Edit: Rebooting the ESP32 does send an update, the problem is of course that the update is all off because it turns off switches when rebooting, and I really don’t need the outboard motor to stop because the mains power is cut after a reboot in the Pi. :grimacing:

If you use MQTT and no retain, then having HA refresh entities will not solve anything. MQTT is push/subscribe, it does not have means to pull the data from the esp.

So the only thing that would work from HA side with MQTT is to have HA send a MQTT message to the esp, and let the esp respond by pushing the required states to HA. I have no idea if that last bit is possible though.

I don’t think there’s another way the esp can see HA went up after a restart.

I would use retain anyway to have a state that is most likely right a.s.a.p. though. Unless it is really bad to have a wrong state for a short while. An/or I’d check if native API is a better fit.

Thanks for clearing that up! I can think of a few situations where it’s really bad, so I want to avoid that. :smile: Pushing the required states is probably the way to go for me.

But do I need the kind of script that I mentioned above and then send topic madmax/hekk/switch/motorheisstr__m/state and payload ON or OFF with an if/then, or is there a way to make the switches report state without “emulating” it? Just to be clear, I’m pretty sure the way I have shown over will work, it’s just a bit of a pain to reprogram all my ESPs with 6-12 relays each.

Are you sure? Because it is possible, if not likely, that when the esp reconnects to MQTT after it was down, that it will send fresh states. Retain was meant for the scenario of reconnects.

Best scenario in any case is to put as much logic inside the esp as possible, so you do not depend on HA in the first place.

I tried to stop the MQTT broker and restart it, but it didn’t report anything beyond what it had already reported when booting.

Edit: That is what I have done, most logic is inside the ESP. It’s mostly triggering actions that is done from Hass. My program for this one ESP is more than 700 lines long. :smile:

Any logic that is essential should be programmed in the esp as much as possible if scenarios can be bad. Because too many components in your setup might fail at one time or another.

Yeah, my thinking too, as I said in my edit above. The 700 lines I’m talking about are in the ESP32. But to trigger stuff (like turning on and off the lanterns from Hass with my big buttons) I need to know the current state.

Here you can see that the lanterns are off (av), but the searchlight is unknown (ukjent).

That means that I am unable to turn on the searchlight until I have been into a totally different view, where there is a regular card with much smaller buttons, and turned it on or off with the lightning symbol:

image

That’s what I’m trying to avoid. So I’m going to use code somewhat like this for each relay. I think I can do it with a common topic that triggers all relays updating, even though I haven’t tried yet.

mqtt:
  on_message:
   - topic: relay_check
     then:
       - if:
           condition:
             switch.is_off: hekkrele2 #("Stern relay 2", which is the same as motorheisstrøm - The ID's are simpler than the "friendly" names)
           then:
             - mqtt.publish:
                 topic: madmax/hekk/switch/motorheisstr__m/state
                 payload: "OFF"
           else:
             - mqtt.publish:
                 topic: madmax/hekk/switch/motorheisstr__m/state
                 payload: "ON"

Yep, that worked. So now I just need to copy that code for every relay. :grimacing:

If a device is mid-reboot then there’s no way to know the state of a physical gpio until it fully boots and that’s why they show unavailable/unknown and a template switch which isn’t physical still shows a state or more accurately, it just shows the last known state and isn’t exactly accurate until connection is established either.

Of course there is, it tells in the switch component documentation.

How you you actually use it, that’s up to you and there are several options such as under on_boot, wifi on_connect, a standalone interval: that just repeats over and over, etc, etc.

I have nothing against mqtt, it’s a great tool for when there aren’t many or any other tools to use but in this case there are so, personally it wouldn’t be my choice here and it sounds like you’ve got a whole mess of services that all add layers in between basically trying to determine the state of a switch and that usually makes things less dependable and more likely to fail because it’s not “A” communicating with “B” it’s now, “A” needs C,G,F to pass the message to “B”. What happens if “G” goes home sick? Things dont work now… Just something to be mindful of.

Sorry, Justin, but you go beside the point in the beginning. The ESPs probably haven’t booted in a few weeks. It’s the Pi that boots (every night at 04), and when it comes back up it has forgotten the state of all the physical GIPO switches. Four hours later, when I get up, it still doesn’t know the state, which is why I will use the code that I showed in my previous message.

And the lambda call is pretty much what I ended up using in the previous post, only I used a bit simpler code, but that shouldn’t matter. Repeating it is not really necessary, all I need is to have it sent when Hass boots, and this code will do that.

And there’s no A, B and C here. More like A (the ESP) B1 (Hass) B2 (Node-RED) B3 (SignalK) and B4 (my Python script). They all work mostly on their own parts of the system (like SignalK taking care of NMEA and sending that to the phone, which is a display on the front steering position for depth, speed and so on) o in parallel (several important functions have a backup or two), not by passing anything to each other, except for the first step. And they are all able to restart everything else if something locks up.

The only thing that I haven’t covered yet is a total freeze of one of the Pi’s that Watchdog doesn’t fix for some rason, but I have already made the box for that. It will check that the script is running on both Pi’s every five minutes. If it’s away twice (so ten minutes), it will cut the power to the frozen Pi and hard restart it. I’m making that even though I haven’t had a freeze like that for two years. The box I have made for that has enough relays to cover all the ESPs as well, so they will be connected when I get that far, probably late fall or early next spring. That’s how paranoid I am about everything working. :grin:

Your using a pi zero to read and store switch states that are actually wired to the esp board?. So, the pi is sleeping 95% of the day and just wakes up to read the switch states from the esp? The same one that doesnt sleep and can do the exaxt thing the pi is doing? WT… Do you sell insurance for a living? Or perhaps build bomb shelters incase Putin comes?

You can easily set those switches up to immediately send state changes to HA or MQTT, whatever. You can also save the states in flash memory inorder to accurately restore them on boot up. That switch state is saved to flash each time it changes. Its so you can restore states on power failure.

Adding sequential numbers to “B” still makes it A,B,C,D because its still multiple (4×) dependencies that all must work and be communicating. The fact a bunch are unnecessary add ons, it kind of proves the point. HAS goes down and so do all the add ons. A single add on freeze, break, or a bug makes it fail to boot then the chain is broken.

To each his own though, if risky and excessive complexity is your thing, do your thing. Im just highly opinionated and trying to help. Either way you wont hurt my feelings if you just tell me to shut it. Thats perfectly fine.

Did I mention a Pi zero? I have never had one of those. And sleeping? Never said that either, I said that it reboots every night. And I didn’t say that is the only thing it does, far from it. The Pi 4 is handling network in, GUI, SignalK, Node-RED, Hass, media playback and the Python script, while the Pi 3 works as a wifi access point, MQTT broker, handles Tellstick (temperature sensors) and GPSD.

And you would not joke with Putin if your wife’s whole family lived 10 kilometers from Russia and about 200 kilometers from Ukraine’s latest strike… :crazy_face: I would guess that there’s a much better chance for him coming to Finnmark in Northern Norway than to whereever you live.

Also the Pi’s and the ESP32’s are on different batteries, so if the auxiliary battery goes down for some reason (like somebody accidentally or on purpose pulling out the charger, that will drain the battery in about two days, while the main/start battery can take a couple of weeks of running the ESPs and the bilge pump) and the scripts in the ESPs does something, Hass does not know that when it boots up. I just wanted Hass to know the state of every switch in the boat on boot. And the short code I wrote does that.

Finally they are not communicating. The four dependencies work totally independent, so if Hass goes down, but not Node-RED, SignalK and the Python script will still work. If node-RED goes down, Hass, SignalK and the Python script will still do it’s thing and so on.

I won’t tell you to shut it, just that my system is working very well and has layers of security, with at least two different systems able to do the same thing, so the boat always works even if one part goes down.

Did you know what time it is here? 4:20AM! The brain isnt operating at 100% so l, did you say that? I made some assumptions. Can i get a little grace this time?
O
Im cool admitting that maybe im not 100% sure what exactly you got going on now and the reasons and purposes for it all. You see similar “overcomplication” from newcomers. I assumed thats what this is and 100% wrong!.. Story of my life though.

Oh geeze dont even get me started on all the wars my country keeps instigating. (USA).

Send the CIA in 2014 to instigate a coup against a democratically elected government because it was pro Ruasia not USA. We knew for a fact Nato membership was a red line not to cross and we did. To add isult to injury we install a comedian as president(a usefule idiot) and we’re(USA) quietly starting the process of installing more offensive nuclear weapons on Russias door step. Of course he was going to invade! He warned us and warned us…

Dont get me wrong, i dont support any of them or war, period. Im just sick of all the propaganda and lies here while the US is turning into a sewer, we import 20mil illegal aliens and have no plan at all what to do with them or even changing laws to allow them to work. Were not even trying to make peace in the East before we start some more crap up again in the middle east after we just left so embarrassingly… who cares about all our problems. “Putin is bad!” “You suckers need to sell your grandchildren’s future so we can start writing checks for war again!”

Were in no business to be telling anyone else whats best when were in decay and most kids in public schools cant read/write or do math at their grade level… people cant afford to live here and suffer. No wonder we lose 175k people a year to drug OD.

Soon as Eu starts thinking for itself and not taking orders from US, the better theyll be off.

No worries, I’m not offended. :grin: And I do disagree about the war. you can believe me in that despite what you may have heard from the GOP side of US politics, the support for the current war is probably 80-90 % in Europe (close to 100 in Eastern Europe, expept for Hungary and Slovakia), much higher than in the US, so in this war it’s more like Europe is trying to light a fire under the US and get them to keep contributing to it. But that is not for this forum. Have a nice day! :innocent:

I appreciate that and respect if you disagree. They are indeed GOP talking points but, which one is not factual?

Call them what you like but were the ones with generations of mamed and disfigured soldiers with PTSD or they become suicide statistics. GOP talking points or not, we just want peace.

I wasn’t going to to go into this, but when you ask: First of all CIA had very little to do with the “coup” which was actually reversal of a coup by a very impopular and very pro-Russia president. He was told by the democratically elected parliament to sign the treaty with EU but decided all on his own to sign one with Russia instead. I have friends in Ukraine who where on Maidan and they laugh at the notion that anybody but the young and middle aged generation were behind it back then.Yanukovych wanted to go towards Russia, almost all outside his party below 60 years of age wanted to go west.

Second, that NATO promised Russia not to accept former East Block countries as member is flat out wrong, even Gorbatchev, who were the president that supposedly was promised to said in an interview that he was never promised that.

The useful idiot is in all of Europe, except for Hungary, considered one of the greatest statesmen since Churchill. Hungary says that he is not democratic president since there should have been an election then, but during any wars in Europe, like WW2, all elections (Norway, Denmark, UK and so on) where delayed to after the war.

Finally NATO didn’t start talking about moving nukes even inside Western Europe until the full scale invasion. Iraq, Afghanistan and Lebanon were clear mistakes, Ukraine is not. But I do know that the GOP (well, Trump party) do not believe what’s on Wikipedia or Brookins, so I think we will stop here.