Hi Tom,
Thanks for your reply, I guess it is one line but.
The file is: hex-dump
I’m interrested in data @ 0x28 and 0x29.
Hi Tom,
Thanks for your reply, I guess it is one line but.
The file is: hex-dump
I’m interrested in data @ 0x28 and 0x29.
Yes, I know.
Phyton is the way to go with I think, but Haas does not accept cut in the syntax.
Or do I have to go with another aproach?
Hi Ondra,
I tried this too. But I can not get it to work with the command line sensor too.
2022-02-15 11:15:19 ERROR (SyncWorker_5) [homeassistant.components.command_line] Command failed: python3 -c “print(open(’/home/hass/.homeassistant/config/downloads/file.hex’).readlines())”
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.
Excuse me for not understanding correctly, here is the right post.
I tried this too. But I can not get it to work with the command line sensor too.
sensor:
- platform: command_line
name: "TempValue"
command: python3 -c "print(open('/home/hass/.homeassistant/config/downloads/data.hex').readlines())"
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.
- platform: command_line
name: "TempValue"
command: "cut -b 1 downloads/data.hex"
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.
Hi Ondra,
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
What am I doing wrong?
Also open is failing…
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))
Yes. Although I wouldn’t put it in python_scripts/
to avoid confusing it with scripts that use the python_script
integration.
If you named your script extract_number.py
, you’ll need to include the .py
extension here as well.
This is a relative path. Please keep in mind that the script will be executed within the configuration directory.
Thx.
I will try it. Im a bit new with the HA environment. It has some limitations in what can be done due to the sandbox. Thx for your help so far.
The relative path must be full path then. How do I know the full path?
The thing sandbox only applies if you use the python_script
integration.
Standard python scripts (or other programs) started by command_line
sensors should be executed inside the homeassistant
container with no other limitations.
That can get quite tricky, since the script runs inside the homeassistant
container. Your configuration directory is mounted as /config
inside that container. I would start by moving your file there, just for testing. You can continue from there after you get the script to work.
It will depend on your installation method.
10 coffee later and it is working !
THX!