Errors Passing Data to Python Script

I have been going in circles trying to get a basic script working, I must be missing something. I have two input_numbers: one for the volume of the TV and one for a slider to control that volume (i have a dumb TV and a broadlink) the python script takes the two numbers and adjusts the volume to be the same so I can use the slider to adjust the volume on the TV.

here is the automation that calls the script:

   alias: TV Volume Down
  description: ''
  trigger:
  - platform: template
    value_template: '{{(states.input_number.tv_vol_slider.state | int) < (states.input_number.tv_volume.state | int)}}'
  condition: []
  action:
  - data_template:
      slider: '{{ (states.input_number.tv_vol_slider.state | int) }}'
      tv: '{{ (states.input_number.tv_volume.state | int) }}'
    service: python_script.tv_volume

here is the python I am using:

tv_vol  = data.get('tv')
slider_vol  = data.get('slider')
vol_dwn = 'JgDMAHI4Dg8OKw4PDg4PDg4PDg4ODw4ODw4ODw4ODg8OKw4PDg4PDg4ODw4ODw4ODg8ODg8rDg4PDg4ODw4ODw4ODw4ODw4rDw0PDg4PDg4OKxANDg8OKw8NDw4ODw8NDyoQDQ8qEAAJiXM4EA0PKg8ODg4PDg4ODw4ODw4ODw4ODg8ODg8OKw8NDw4ODw4ODw4ODg8ODg8ODg8qEA0ODw4ODw4ODg8ODg8ODg8qDw4ODw4ODw4OKw8ODg4PKg8ODg8ODg8ODisQDQ4rDwANBQAAAAAAAAAAAAAAAA=='
vol_up = 'JgAyAXM4Dw4OKxANDg4PDg4ODw4ODw4ODw4ODw4ODg8OKw4PDg4ODw4ODw4ODg8ODg8ODg8qDw4ODw4ODw4ODw4ODg8ODg8ODg8ODg4PDg4PKg8ODg8ODg8ODg8ODg4PDisODw4rDgAJgXM4Dg4PKw4ODg8ODg8ODg8ODg4PDg4PDg4PDg4OKw4PDg8ODg4PDg4PDg4PDQ8ODw4rDg8ODg8ODRAODg4PDg4ODw4PDg4ODw4ODw4OKw4PDg4PDg4PDg4ODw4ODyoPDg4rDwAJgXI4Dw4OKw4PDg4PDg4PDg4PDg4PDg4ODw4ODg8OKw4PDg4ODw4PDg4ODw4ODw4ODw4rDQ8PDg4PDg4ODw4ODw4ODw4ODg8ODg8ODg8OKw4ODw4ODw4ODg8ODg8ODisPDg4rDgANBQAAAAAAAA=='


if (tv_vol > slider_vol):
    num_loop = tv_vol - slider_vol  
    service_data = {'host': '10.0.0.12', 'packet': vol_dwn}
    for x in range(num_loop):
        hass.services.call('broadlink', 'send', service_data, 'False')
        time.sleep(0.5)
    hass.states.set('input_number.tv_volume', slider_vol, 'False')
else:
    if (tv_vol < slider_vol):
        num_loop = slider_vol - tv_vol  
        service_data = {'host': '10.0.0.12', 'packet': vol_up}
        for x in range(num_loop):
            hass.services.call('broadlink', 'send', service_data, 'False')
            time.sleep(0.5)
        hass.states.set('input_number.tv_volume', slider_vol, 'False')
    else:
        pass

and here is the error I get…

Log Details (ERROR)
Logger: homeassistant.components.python_script.tv_volume.py
Source: components/python_script/__init__.py:205
Integration: Python Scripts (documentation, issues)
First occurred: 2:14:22 PM (1 occurrences)
Last logged: 2:14:22 PM

Error executing script: unsupported operand type(s) for -: 'str' and 'str'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
    exec(compiled.code, restricted_globals)
  File "tv_volume.py", line 8, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Even though you think you’re passing in two integers it’s really converting them to strings. The error says you’re trying to subtract two strings. So remove the |int from the two input parameters and let it pass strings then convert to integers on the other end like so.

tv_vol  = int(data.get('tv'))
slider_vol  = int(data.get('slider'))

For the calling script

      slider: "{{ states('input_number.tv_vol_slider') }}"
      tv: "{{ states('input_number.tv_volume') }}"

For the python_script you could make things a little more readable using elif and no parens required.

elif tv_vol < slider_vol:

ok that got me closer, but still gedtting this one:

Logger: homeassistant.components.python_script.tv_volume.py
Source: components/python_script/init.py:205
Integration: Python Scripts (documentation, issues)
First occurred: 11:46:10 AM (1 occurrences)
Last logged: 11:46:10 AM

Error executing script: invalid literal for int() with base 10: ‘16.0’

Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 205, in execute exec(compiled.code, restricted_globals) File “tv_volume.py”, line 1, in ValueError: invalid literal for int() with base 10: ‘16.0’

ok well the string is coming in as a float X.XX something. So either convert to float first then integer or use split(’.’)[0] to cut off the decimal and grab the whole number and convert to integer like this

tv_vol  = int(data.get('tv').split('.')[0])
slider_vol  = int(data.get('slider').split('.')[0])

or similarly

tv_vol  = float(data.get('tv'))
tv_vol = int(tv_vol)
slider_vol = float(data.get('slider'))
slider_vol  = int(slider_vol)

Or just leave it as a float as it won’t hurt anything and the math will work.The sandbox python can be difficult. Sometimes it throws a fit if you do too many operations in one statement.

I think that has it, but i’m rebuilding HA on my new Pi 4 so we will see :slight_smile: thanks for the help