Trouble using a 'device_id' field in a script, works for other types of field

I’ve automated my window shutters using a KaKu shutter control relay. To enable code re-use (we have 10 window shutters), I’m trying to create generic scripts to command the respective shutter to “open”, “close” or “partial open for ventilation”.

As part of this, I need to pass a parameter containing the device ID for the rolling shutter. I’m also passing 2 parameters with the time parameters for opening/closing.

I’ve implemented this using the “fields” construct in the script. This construct seems to work for the time parameters, but not for device_id parameter.

When I attempt to replace the hardcoded device ID with the variable “rolluik_id”, I cannot save the script, with errors similar to

Message malformed: Unknown device '{{ rolluik_id }}'"

So this works:

  - device_id: 9940ab32e65f6166bd4e12a931956f02

But parametrisation does not work in any form:

  - device_id: {{ rolluik_id }}
  - device_id: '{{ rolluik_id }}'
  - device_id: "{{ rolluik_id }}"

I’ve read up on “Script Syntax”, “Dynamic Entity ID in Scripts” etc. to no avail. I cannot seem to understand why referencing a fields entry ‘rolluik_ventilatie_open_s’ works, but not a fields entry for ‘rolluik_id’.

Full script code below (fails to save after I replaced hardcoded ID in first sequence with field variable:

alias: rolluik_ventilatiestand
sequence:
  - device_id: "{{ rolluik_id }}"
    domain: rfxtrx
    type: send_command
    subtype: "On"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ rolluik_ventilatie_sluit_s }}"
      milliseconds: 0
  - device_id: 9940ab32e65f6166bd4e12a931956f02
    domain: rfxtrx
    type: send_command
    subtype: "Off"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ rolluik_ventilatie_open_s }}"
      milliseconds: 0
  - device_id: 9940ab32e65f6166bd4e12a931956f02
    domain: rfxtrx
    type: send_command
    subtype: "Off"
description: Open het rolluik
icon: mdi:window-shutter-settings
fields:
  rolluik_id:
    description: Device ID van het rolluik
    default: 9940ab32e65f6166bd4e12a931956f02
    selector:
      device:
        integration: rfxtrx
  rolluik_ventilatie_sluit_s:
    description: Ventilatiestand, rolluik compleet sluiten (s)
    default: 25
    selector:
      number:
        min: 20
        max: 30
        step: 1
        unit_of_measurement: s
        mode: slider
  rolluik_ventilatie_open_s:
    description: Ventilatiestand, rolluik openen (s)
    default: 6
    selector:
      number:
        min: 0
        max: 10
        step: 1
        unit_of_measurement: s
        mode: slider
mode: single

The reason for the error message is because Device Triggers, Conditions, and Actions do not support templates.

For your application, replace the Device Action with a service call (it supports templates).

Thanks, let me try that. I rewrote using the Service call - this works with the literal entity_id but failed when parametrising the entity_id:

service: switch.turn_on
data: {}
target:
  entity_id: '{{ rolluik_id }}'

with:

fields:
  rolluik_id:
    description: Entity ID van de rolluik-schakelaar
    default: switch.schakelaar_l_rolluik_kantoor_links

resulted in:

Error: Template rendered invalid entity IDs:

My guess would be that I need to do some type casting - but I’m probably wrong :wink:

The error message indicates the value that was supplied for rolluik_id was not a valid entity_id. What was the value?

I’m using the “Run script” command in the overflow menu of the script editor, so I’d expect the value to be the default: switch.schakelaar_l_rolliuk_kantoor_links

As you have discovered, it doesn’t work that way.

Test your script via Developer Tools > Services. I suggest you initially provide a value for rolluik_id (i.e. test one thing at a time).

Yup, I’ve discovered lots of ways that did not work :wink:
Thanks for your help, I’ll go test via Developer Tools!

You’re welcome!

Please consider marking my first post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

As indicated by @123, running the script via Developer Tools > Services works.

For completeness, here is the complete and working script:

alias: rolluik_ventilatiestand
description: Zet rolluik in ventilatie-stand
icon: mdi:window-shutter-settings
fields:
  rolluik_id:
    description: Entity ID van de rolluik-schakelaar
    default: switch.schakelaar_l_rolluik_kantoor_links
    selector:
      entity:
        filter:
          - integration: rfxtrx
  rolluik_ventilatie_sluit_s:
    description: Ventilatiestand, rolluik compleet sluiten (s)
    default: 25
    selector:
      number:
        min: 20
        max: 30
        step: 1
        unit_of_measurement: s
        mode: slider
  rolluik_ventilatie_open_s:
    description: Ventilatiestand, rolluik openen (s)
    default: 6
    selector:
      number:
        min: 0
        max: 10
        step: 1
        unit_of_measurement: s
        mode: slider
sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: "{{ rolluik_id }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ rolluik_ventilatie_sluit_s }}"
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: "{{ rolluik_id }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ rolluik_ventilatie_open_s }}"
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: "{{ rolluik_id }}"
mode: single

Edit: pasted an old version of the script without parametrized delays.

The script contains two sequential switch.turn_off service calls (separated by a delay). Is the need for the second one in case the first one fails to work?

Hi, no, this is a limitation / feature of the KaKu relays I’m using. The first “Off” command tells the shutter to open. The second “Off” command tells the shutter relay to stop moving.

In effect, the ventilation position is achieved by moving the shutter down (“On”) completely, then moving it up (“Off”) for a couple of seconds to slightly open the shutter.