How to call a script from python_script integration

Hi there everyone,

I am using the standard python_script integration from HA and I have an automation set up to monitor MQTT topic from openzwave for certain events. The automation got triggered fine and fired off the python script. However the call stopped here, my script did not get run. I think I am missing the important information as how to call the script service from python.

Here are the python codes:

event = data.get('event')
label = data.get('label')
index = data.get('index')
node_id = int(data.get('node_id'))

# we only care about index == 10, the end of the swipe 'parameter'
# and for safety we also only look at valueChanged
if (index == '10') and event == 'valueChanged':
    value = int(data.get('value', '0'))
    # unpack byte 2 (button id)
    scene_id = value >> 24 & 0xff

    # unpack byte 3 (direction)
    scene_value_id = value >> 16 & 0xff
    # offset by 10 to avoid colissions
    scene_value_id = scene_value_id + 10

    scene_value_label = 'Swipe Down'
    if scene_value_id == 11:
        scene_value_label = 'Swipe Up'

    if '7' in node_id:
      node = 'bedroom'
    if '4' in node_id:
      node = 'livingroom'

    if 'livingroom' in node:
      if '1' in scene_id:
        light_id = 'light.living_room_lights'
      if '2' in scene_id:
        light_id = 'light.dining_lights'
      if '3' in scene_id:
        light_id = 'light.corner_lights'
      if '4' in scene_id:
        light_id = 'light.stairs_lights'
    elif 'bedroom' in node:
      if '1' in scene_id:
        light_id = 'light.bedroom_lights'

    swipe_action = scene_value_label

    hass.services.call('script', 'turn_on', {
      'entity_id': 'script.dimmer',
      'light_id': light_id,
      'swipe_action': swipe_action,
    }, False)

I would like to know how to propertly call the script.dimmer from python. light_id and swipe_action are variables for the script.

The automation:

- alias: Wallmote Swipe Handling
  trigger:
    platform: mqtt
    topic: "OpenZWave/1/node/+/instance/1/commandclass/112/value/+/"
  action:
    - service: python_script.wallmote_handler
      data_template:
        node_id: "{{trigger.payload_json['Node']}}"
        event: "{{trigger.payload_json['Event']}}"
        value: "{{trigger.payload_json['Value']}}"
        index: "{{trigger.payload_json['Index']}}"

The script:

  dimmer:
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: '{{ light_id }}'
          brightness: >-
            {% set domain, name = light_id.split('.') %}
            {% set current = states[domain][name].attributes.brightness | int %}
            {% set step = states('input_number.light_step') | int %}
            {% set action = swipe_action %}
            {% if action == 'Swipe Up' %}
              {% set next = current + step | int %}
            {% elif action == 'Swipe Down' %}
              {% set next = current - step | int %}
            {% endif %}
            {% if next > states('input_number.light_maximum') | int %}
              {% set next = states('input_number.light_maximum') | int %}
            {% elif next < states('input_number.light_minimum') | int %}
              {% set next = states('input_number.light_minimum') | int %}
            {% endif %}
            {{ next }}

If I tried to run script.dimmer with the variables light_id and swipe_action from the frontend UI, it works. So somehow the call to script.dimmer is either not correct or maybe I am missing something here.

I verified the codes separately and the variables are correctly parsed and passed to the script as you can see in the log:

2021-01-15 10:31:01 INFO (SyncWorker_9) [homeassistant.components.python_script] Executing wallmote_handler.py: {'node_id': 4, 'event': 'valueChanged', 'value': 33649865, 'index': 9}
2021-01-15 10:31:01 INFO (MainThread) [homeassistant.components.automation.wallmote_swipe_handling] Wallmote Swipe Handling: Running automation actions
2021-01-15 10:31:01 INFO (MainThread) [homeassistant.components.automation.wallmote_swipe_handling] Wallmote Swipe Handling: Executing step call service
2021-01-15 10:31:01 INFO (SyncWorker_12) [homeassistant.components.python_script] Executing wallmote_handler.py: {'node_id': 4, 'event': 'valueChanged', 'value': 33671114, 'index': 10}

I got the python code from here.

Hopefully someone knows more about this and can help me with this. Thank you so much!

Update 15 Jan 2021

The solution to the python script is here.

index isn’t a string

1 Like

Hi Petro,

Oh… thanks for pointing that out. I just reviewed the code and fix the bugs. Actually I was missing the variables in the calling routine. Now everything works: I can use the Zwave Wallmote Quad swipe function to dim the lights. I am still learning python… :wink: I will report back to the original coder and update this post accordingly.

Thank you so much Petro for your help!

This is the modified code:

event = data.get('event')
label = data.get('label')
index = data.get('index')
node_id = int(data.get('node_id'))

# we only care about index == 10, the end of the swipe 'parameter'
# and for safety we also only look at valueChanged
if (index == 10) and event == 'valueChanged':
    value = int(data.get('value', '0'))
    # unpack byte 2 (button id)
    scene_id = value >> 24 & 0xff

    # unpack byte 3 (direction)
    scene_value_id = value >> 16 & 0xff
    # offset by 10 to avoid colissions
    scene_value_id = scene_value_id + 10

    scene_value_label = 'Swipe Down'
    if scene_value_id == 11:
        scene_value_label = 'Swipe Up'

    if node_id  == 7:
      node = 'bedroom'
    if node_id  == 4:
      node = 'livingroom'

    if 'livingroom' in node:
      if scene_id == 1:
        light_id = 'light.living_room_lights'
      if scene_id == 2:
        light_id = 'light.dining_lights'
      if scene_id == 3:
        light_id = 'light.corner_lights'
      if scene_id == 4:
        light_id = 'light.stairs_lights'
    elif 'bedroom' in node:
      if scene_id == 1:
        light_id = 'light.bedroom_lights'

    swipe_action = scene_value_label

    hass.services.call('script', 'turn_on', {
      'entity_id': 'script.dimmer', 'variables': {
      'light_id': light_id,
      'swipe_action': swipe_action,
    }}, False)

I use this script together with the automation in this post to control the Aeotec Quad Wallmote with OpenZwave and HA python_script integration. It eliminates the need to run a python script in the background to parse OZW.log (with built-in zwave integration), in this post.