Command line sensor: requests module (Python) no longer available

I have an custom set of sensors that read data from my Solvis Max controller. It is build using a command line sensor like this:

 - platform: command_line
      name: SolvisMax
      command: "python3 /config/scripts/solvis.py"
      json_attributes:
        - Anlagentyp
        - Systemnummer
        - Tank_Top
        - Warmwasser
        - Tank_Bottom
        - Tank_MidTop
        - HeatPump_Feed
        - HeatPump_Back
        - S7
        - HeatPump1
        - Tank_MidBottom
        - OutsideTemp
        - WWCycleBack
        - HK1_Feed
        - HK2_Feed
        - BoilerSensor
        - HeatPump2
        - S16
        - VolumeWW
        - Volume_Heatpump
        - S19
        - S20
        - S21
        - Modulation_Boiler
        - S23
        - S24
        - S25
        - S26
        - S27
        - S28
        - Pump_HeatPump
        - Pump_WW
        - Pump_HK1
        - Pump_HK2
        - Pump_WWCycle
        - Pump_HK3
        - Pump_Solar2
        - HK1_Mix_Open
        - HK1_Mix_Close
        - HK2_Mix_Open
        - HK2_Mix_Close
        - Boiler_NA
        - Charge_Pump_Boiler
        - Boiler
        - Modulation_Boiler_Soll
        - Modulation_Delta
       value_template: "{{ state_attr('sensor.solvismax', 'Tank_Top') }}"  
      unit_of_measurement: "°C"

the python script that decodes the data from the controller worked for over one year, but stopped working some weeks ago (I keep my Homeassistant as current as possible).

#!/usr/bin/python3

import requests
from requests.auth import HTTPDigestAuth



def hextoint ( s ):
  
#  print ("hextoint s="+s)
  if len(s) > 2:
    h = s[2:4]+s[0:2]
  else :
    h = s[0:2]
#  print ("hextoint h="+h)
   
  return int ( h, base = 16)
    

r = requests.get('http://solvis.xxxxx.xx/sc2_val.xml', params = {'dummy':'42'} , auth = HTTPDigestAuth('admin', 'xxxxx'))

start = r.text.find("<data>")
ende = r.text.find("</data>")

s = r.text[start + 6 : ende]

s = s[18:]

print( '{')

print ( ' "Anlagentyp":"'+str(hextoint(s[0:4]))+'",')
s = s[4:]

# Decoding of the message follows here, quite long

I seems that the requests module has been removed.
But even the documentation of the command line sensor suggests using a python script to poll values from a remote device. https://www.home-assistant.io/integrations/command_line#use-an-external-script
I there a way to re-install this module ?

If not, you could curl the XML and pipe it into a de-requested version of the script.

You can always manually install the request module with command “python3 -m pip install request” use this command with the an automation on HA start, so it gets installed in the home assistant container

Found it!

Tried everything - curl and pipe that into the script etc. Trying to install the requests (Note the ‘s’ at the end) package. But that it wasn’t.
then did a docker exec -it homeassistant /bin/bash
and found that python has been moved to /usr/local/bin.

We should note to start python scripts with
#!/usr/local/bin/python

1 Like

… But my sensors still don’t work. When I go into the homeassistant container (see above), I can run the script and it produces that JSON output that gets parsed into the values of the sensors.
In the log there is no error caused by the script:

2023-05-12 18:44:04.380 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.command_line
2023-05-12 18:44:04.380 DEBUG (SyncWorker_39) [homeassistant.components.command_line.sensor] Running command: /config/scripts/solvis.py
2023-05-12 18:44:04.539 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform command_line
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state numerical_value = float(value) # type:ignore[arg-type]
ValueError: could not convert string to float: 'None'
The above exception was the direct cause of the following exception:
.....

All that worked a few versions ago…