Hi folks, so I have a substantial number of comand line sensors.
And I now have to move them all to the new format. So I thought, there might could be a more easy / elegant way of doing that…
Right now I have set up 6 sensors per machine. Each is defined by its self. Quite some copy paste work, with lots of errors to debug in the beginning, but working fine since monthes now.
“MY” coding looks like this… crude:
- platform: command_line
name: S19-31-Inlet-Temp-Board-1
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp1 }}'
- platform: command_line
name: S19-31-Outlet-Temp-Board-1
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp2_1 }}'
- platform: command_line
name: S19-31-Inlet-Temp-Board-2
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp2 }}'
- platform: command_line
name: S19-31-Outlet-Temp-Board-2
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp2_2 }}'
- platform: command_line
name: S19-31-Inlet-Temp-Board-3
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp3 }}'
- platform: command_line
name: S19-31-Outlet-Temp-Board-3
command: "python3 /config/shell/S19-31.py"
unit_of_measurement: "°C"
value_template: '{{ value_json["STATS"][1].temp2_3 }}'
as you can see, its exactly the same asside from
- inlet/ outlet and Bord number in the nameing, and
- temp1 (inlet) / temp2_1 (outlet)
for each board (3 of them in each machine…
on the python side it looks like this
#!/usr/bin/env python3
# https://community.home-assistant.io/t/read-antminer-api-and-show-details/123795/4
import socket
import json
HOST = 'IP.of.machine'
PORT = 'port'
m = {"command": "stats"}
jdata = json.dumps(m)
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(bytes(jdata,encoding="utf-8"))
data = ''
while 1:
chunk = s.recv(4026)
if chunk:
data += chunk.decode("utf-8")
else:
break
datajs = json.loads(data[:-1])
finally:
s.close
print (json.dumps(datajs))
I have a copy of this script for each machine…
the only change is the last numbers of the ip address.
how could i get that more elegant ? like just one script and send the ip with the command line sensor or what ever, give it a range, to poll say IP .180 to .196 or what ever, like hand over a list of IPs to querry?