Home Assistant & MySensors: why set state when initialising?

I’m only a few days into using Home Assistant and have loved the experience so far! Thanks to everybody involved for the amazing work!

I’m currently trying to hook my Arduino up to my hass-running raspberry pi via serial (USB). I was about to invent my own protocol, but then learnt about MySensor - another amazing piece of work!

So, basically, my question is: when using mysensor in home assistant, why do we have to set the state a sensor when we first present it? (as documented https://home-assistant.io/components/mysensors/#presentation).
As far as I can tell, this is not a requirement on mysensor, nor indeed pymysensor, so assume it is relating to the integration of mysensor and home-assistant? Is it something I could work around? I’m happy to get my hands dirty in the code, and would be more than happy to submit a PR if this is behaviour that is not intrinsic to my setup.

Full context:
I have some RF433 controlled relays that would work perfectly with the rpi_rf (https://home-assistant.io/components/switch.rpi_rf/) switch platform, except I want to hook them up to an arduino running a mysensor gateway. Comms will be Serial, and the arduino won’t be storing the state of the switches - I was hoping, since these relays are “assumed state”, that I could make use of hass to store the state. That means that from my Arduino I want to present the switch, then request what state it should be set to by the controller. Unfortunately, hass doesn’t see the switch unless I actually set a value, thus overwriting any state that may have been preserved.

Good question. @martinhjelmare has been the developer/maintainer for this plugin; maybe check with him?

Currently we need to know the type of value from a device to know how to handle the device. That will always be required to make the entity functional in home assistant, but I think it could be delayed after adding the entity in home assistant. I’m working on a branch about that.

Sending an initial value is only required to be done one time, if you use persistence. So I think you could try to request a value from home assistant during initialization of the device in the loop, then if you don’t get a response after a set time, you send an initial value and move on.

PS: I’m also working on a branch where you can set the initial values of the devices from home assistant config.

1 Like

Just wanted to follow-up and confirm that it is indeed possible to avoid sending an initial state if home-assistant already knows about the device. I basically have a timer in my loop code that checks to see if a period of time has passed (20 seconds) since start-up without receiving a message about a switch, and if there hasn’t, I send over an initial state.

Thanks for your help. I’ll post my entire setup over the next few months - making use of the mysensors platform quite extensively.


1 Like