Python script help

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)

I hope someone can help…

Closest I have got is:

temp = [str(temp)]

But this gives me a weird string with:
[“b’
added to the begging of the string and this:
\n’”]
added to the end…

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

I’m trying to get whole text outputs into home assistant not just numbers/temps

So for example I want to get a brand name into Home Assistant “Asus” so I need the script to just pass the text/numbers as it sees them…

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)

The temp=subprocess.check_output I am using is a text string of multiple words

I need this to be sent to home assistant but the current coding allows only numbers

Does this make more sense?

Then please show the file / output…with me it is just providing
[“b’54000\n’”]

One example would be:

[“b’Asus\n’”]

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)

Maybe I just badly explained what I was trying to accomplish

I’m not trying to get a temperature at all

I am actually trying to run code like this:

“dmidecode | more | head -1 | tail -1 | awk ‘{print $1}’”

instead of this:

“cat /sys/class/thermal/thermal_zone3/temp”

How can I make this work?

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

I honestly thought I had given the idea in the op but clearly I did not so I have hopefully now given all the information needed, apologies

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

  1. command(line) and ‘why’
  2. expected / known output that you have
  3. 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.

dougiteixeira/proxmoxve: Proxmox VE Custom Integration Home Assistant (github.com)

Finally got it to work, crude but this just removes the bits at the front and back so only what I need is displayed:

#!/usr/bin/env python3

import subprocess
from requests import post

value=subprocess.check_output("dmidecode | more | head -1 | tail -1 | awk ‘{print $1}", shell=True)

value = str(value)[2:-3]

url = "http://192.168.1.53:8123/api/states/input_text.value"

headers = {
  "Authorization": "Bearer ABCDEFG",
  "content-type": "application/json"
}

data = '{"state": "%s" }' % value

response = post(url, headers=headers, data=data, verify=False)

Thanks for the help