Hello all,
I am adding an interface to a charging point for an electric vehicle to my Home Assistant setup. I encounter an ‘interesting’ issue in the sense that I can read the status of various sensors in the charging point hardware but I can’t create an automation that runs as expected when a specific state change occurs.
The device itself is connector over Modbus TCP/IP, so I need to define the addresses and field types for that various registers I want to read out. There is a specific register called mode3_state
that presents the charge point status as a text string. This allows me to check if there is a car connected to the charge point or not, and if it is charging or not. The status values of interest to me are ‘E’ (meaning no car is connected) and ‘A’, ‘B2’ and ‘C2’ (meaning a car is connected, if power is supplied to the car of if it is actually charging).
Now, onto what I want to implement: as soon as a car is connected at night I want to enable the front lights of the house. I thought this morning this would be easy: just create an automation that triggers on a state change of the mode3 sensor and be done with it. Of course it did not work as quickly as expected …
Onto the ‘interesting’ part. I found out that as soon as I added a ‘from’ or ‘to’ condition to the trigger the automation would not run. I would see the value of the charing point mode3 sensor change on the web interface of Home Assistant, but it would not trigger the automation.
So I added a notification to the automation to print the state of the sensor. Also this would work as expected and print what I expected over the notification when there was not ‘to’ or ‘from’ restriction. But as soon as I added one the automation would not trigger. Hmmm…
I found out using the ‘step details’ trace of the automation that the sensor state contains a few trailing null characters ("\0"). Apparently those null characters are not stripped from the sensor value when evaluating the trigger condition of the automation. Those trailing zeros were not transmitted via the notification system (a web hook which strips the \0) so I had no clue they were there. The fact that the trailing null characters are there are due to the way the modbus interface works: I need to define how many bytes to read from the register address, and this is specified by the manufacturer of the device so I cannot really change that.
So OK, it was then clear why the automation was not triggered when I added a ‘to’ or ‘from’ rule. When I state that the automation should run whenever the sensor state changes from E
to some other value, then it is logical that it is not triggered if the actual sensor state is E\0\0\0\0\0\0\0\0
.
On the other hand: the fact that the sensor state has in fact trailing null characters is not easily visible in the user interface of Home Assistant (not in the web UI and not in the ‘development tools’ section of the web interface). It was only after printing the sensor state to a notification and using the automation step details page that this became clear.
My question is: what is the most logical way to fix this? Can I easily work around this by changing how I define the trigger rules for the automation? Any tips and tricks are very welcome.
Second thought: should Home Assistant attempt to ‘sanitise’ sensor values and strip trailing \0 by default to avoid that users are scratching they heads trying to figure out why an automation is not triggered as expected?
Thanks,
Hollie