I’m trying for the past few days to get 2 bytes out of a binairy file.
I’m not certain how to approach this. I have tried de file sensor with cut -b 40, 41 or cut -b 40-41. But I cannot get the read the data.
I have tried with pyton but io.open does not work correctly.
Can anyone point me in the right direction or have an example? The file is locatied in the downloads folder.
Yeah, it did not say to post pictures of text. Please don’t do that. It makes it very difficult for people trying to help you copy your config to edit it. Just follow the instructions in the link.
I found out some other issues regarding files/directories etc.
Now the file can be accessed. I simulated with test.txt with the text “hello”.
I can now read the chars correctly, but with the binairy file I need some help.
How to get a hex-value of the data and make it an integer and do some caluclations with it.
I don’t think Home Assistant will be able to work with binary data (non-printable characters) properly.
You’ll need to write a script (Python, shell or whatever) that prints the resulting integer to stdout and add that as a command_line sensor. If you put your script in <configuration directory>/my_scripts/script, you should use command: /config/my_scripts/script.
Either specify the interpreter as command: python3 /config/my_scripts/script, or make the script executable and add a proper shebang.
Thanks for you reply. Can you give me an example of how to accomplish what you wrote?
Can it be done with the standard Home Assistant python or do I have to install pyscript? AppDeamon?
No need to use pyscript or anything like that.
You’ll need to create a script somewhere in your configuration directory (next to configuration.yaml). Let’s call it extract_number:
#!/usr/bin/env python3
import struct
# open the file, extract the number (possibly using struct.unpack)
# print the resulting integer to stdout
print(1234)
You can then create a command line sensor:
sensor:
- platform: command_line
name: My sensor
scan_interval: 30 # run the command every 30 seconds
command: /config/extract_number
#!/usr/bin/env python3
import struct
# open the file, extract the number (possibly using struct.unpack)
f = open("downloads/data.hex","rb")
f.seek(40)
data = f.read(1)
print (struct.unpack("B",data))
gives the following error:
2022-03-09 12:50:05 ERROR (SyncWorker_1) [homeassistant.components.python_script.extract_number.py] Error executing script: import not found
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 224, in execute
exec(compiled.code, restricted_globals)
File “extract_number.py”, line 3, in
ImportError: import not found
2022-03-09 13:29:01 ERROR (SyncWorker_3) [homeassistant.components.python_script.extract_number.py] Error executing script: name ‘open’ is not defined
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 224, in execute
exec(compiled.code, restricted_globals)
File “extract_number.py”, line 5, in
You seem to be running the script using python_script. It’s documentation says
The scripts are run in a sandboxed environment.
…
It is not possible to use Python imports with this integration. If you want to do more advanced scripts, you can take a look at AppDaemon or pyscript
That’s one of the reasons why I recommended that you use a command line sensor instead. It should work just fine, and I see no reason to use python_script since you don’t need to work with the hass object anyway.
So just call the script via the commandline sensor,
sensor:
# test with python
- platform: command_line
name: "PyTest"
scan_interval: 5 # run the command every 5 seconds
command: /config/python_scripts/extract_number
with the py-script?
#!/usr/bin/env python3
import struct
# open the file, extract the number (possibly using struct.unpack)
f = open("downloads/data.hex","rb")
f.seek(40)
data = f.read(1)
print (struct.unpack("B",data))