I really like the simpleness of the script found here:
and could use it to send all sorts of handy information to home assistant but for the life of me I can’t work out how to get it to send whole text strings instead of filtering out and sending only numbers. I know I need to change it to input_text instead but I don’t understand what to change in the “temp” field to get it to send the correct information
Heres the code:
#!/usr/bin/env python3
import subprocess
from requests import post
# Get temperature from system using the command from step 6
temp=subprocess.check_output("cat /sys/class/thermal/thermal_zone3/temp", shell=True)
# Parse temperature value from string
temp = [int(i) for i in temp.split() if i.isdigit()][0]
# Replace hostname and port with your HA instance
# Make sure to use the correct protocol, http or https
url = "http://192.168.1.53:8123/api/states/input_number.cpu_temp"
# Replace ABCDEFG with your Long-lived access token
headers = {
"Authorization": "Bearer ABCDEFG",
"content-type": "application/json"
}
data = '{"state": %s}' % temp
response = post(url, headers=headers, data=data, verify=False)
It may help if you post what temp output is and what you want to add where, if it is just a single temperature value then you can continue to use the input_number
EDIT: script runs 100% fine for me, just added the token and correct ip
The value needs to be divided by 1000 but that is a small change
Still not sure what you want but try this, may help getting an idea for you
#!/usr/bin/env python3
import subprocess
from requests import post
# Get temperature from system using the command from step 6
temp=subprocess.check_output("cat /sys/class/thermal/thermal_zone1/temp", shell=True)
# Parse temperature value from string
temp = [int(i) for i in temp.split() if i.isdigit()][0]
# Replace hostname and port with your HA instance
# Make sure to use the correct protocol, http or https
url = "http://192.168.1.53:8123/api/states/input_text.cpu_temp_somename"
# Replace ABCDEFG with your Long-lived access token
headers = {
"Authorization": "Bearer ABCDE",
"content-type": "application/json"
}
t1 = temp
t2 = "some text"
data = '{"state": " %s %s " }' % (t1, t2)
response = post(url, headers=headers, data=data, verify=False)
How would a temp file show that? I donot think to understand the overall goal you have. this then?
import subprocess
from requests import post
# Get temperature from system using the command from step 6
temp = subprocess.run(['cat', '/sys/class/thermal/thermal_zone3/temp'], check=True, capture_output=True, text=True).stdout
# Replace hostname and port with your HA instance
# Make sure to use the correct protocol, http or https
url = "http://192.168.1.53:8123/api/states/input_text.cpu_temp_somename"
# Replace ABCDEFG with your Long-lived access token
headers = {
"Authorization": "Bearer ABCDE",
"content-type": "application/json"
}
data = '{"state": " %s " }' % temp
response = post(url, headers=headers, data=data, verify=False)
So how should I have guessed this?
Then, you continue to send code that apparently does not work instead of sending the data, its source and what you want to achieve… I am not a clairvoyant…maybe others are but motivation is sinking
So… WHAT do you want to achieve… please be specific as it may not even be me that can help you. Generic questions are for management meetings…then you would have to pay me (dearly)
Basically I want to use the below code to send information from Linux/proxmox to home assistant. Ignore that is says “temp” this can be changed to anything later. The information I’m trying to send is basically text, sometimes number, mainly text. So for example if I wanted to send home assistant the manufacturer of my motherboard the information I would be sending is “Asus”.
The below code does work but gives me a weird string with:
[“b’
added to the begging of the string and this:
\n’”]
added to the end
Also home assistant doesn’t update the “last changed” time if the same data is resent although this is not important
Hopefully this clears everything up, thank you
#!/usr/bin/env python3
import subprocess
from requests import post
# Get temperature from system using the command from step 6
temp=subprocess.check_output("dmidecode | more | head -1 | tail -1 | awk ‘{print $1}", shell=True)
# Parse temperature value from string
temp = [str(temp)]
# Replace hostname and port with your HA instance
# Make sure to use the correct protocol, http or https
url = "http://192.168.1.53:8123/api/states/input_number.cpu_temp"
# Replace ABCDEFG with your Long-lived access token
headers = {
"Authorization": "Bearer ABCDEFG",
"content-type": "application/json"
}
data = '{"state": %s}' % temp
response = post(url, headers=headers, data=data, verify=False)
I already indicated in an earlier post on how to deal with strings via ‘data’.
Your cli
dmidecode | more | head -1 | tail -1 | awk ‘{print $1}
does not return anything much with me so, maybe related to OS or setup … so not sure how / what this would need to do on a data level.
I am reapeating earlier requests , probably I am not specific too, so…do add
command(line) and ‘why’
expected / known output that you have
what / where do you want to put it (or parts of it) in HA
Unless you just want the challenge there is a Proxmox integration with a lot of stats. If nothing else the code here may give you pointers on what you want to achieve.