A list with 2 entries reportedly comes up.
How can I get the first and the second entry now?
If I enter {{ states('sensor.test_modbus')[0]}} I would expect 5000. But it only gives me back “the first letter” i.e. 5.
That will work if all of your registers are int16.
But if you have a mixture of int16 and uint16 you will need to use the split method. As ideally you want to limit the amount of Modbus requests you do.
You will put a lot less load on the Modbus device doing a single request for 100 registers in a block compared to requesting 50 blocks of 2 registers for example.
Yes, I know they have to have the same type. For another block I need to read 8x uint32 and 1x int16. But I have still no idea how to define the structure. Where can I find this information? Thank
Going back to your first post, the answer is in “See Python documentation for details” so look for ‘Python struct documentation’.
HA is, I believe, mostly written in Python. To process a data buffer, the Python language has a library (module of code) called ‘struct’ that can be called upon to do the hard work of packing and unpacking data into or from a buffer.
in particular the bit on Format Strings, and format characters provides all the information you require.
To make it work, ensure that the amount of data read (the buffer size) matches the unpacking size of the format string, make sure that you have the correct endian for your Modbus device, and make sure you choose signed/unsigned correctly.
My guess is that you want something like (with a buffer read of 17)
">8Lh"
And if you are still confused about the “double quote”, then " is a double quote, and ’ a single quote. Both are commonly used to be able to quote a string within a string - hence ‘structure: “>8LH” ’ can be unpicked by a computer into a string within a string (I added a space between " and ’ as "’ is rather difficult to read)
Thank you. Yeah… I realised late that it was the documentation of “struct”. Probably while you were writing the answer
Well, it works. I have defined the following structure:
“>8Li”
Here is the yaml: