[Solved] script with variable

Hello to all,

I’m trying to use the RFPlayer to control RTS shutters. The integration works well and I can control them.

But in lovelace cards, I can’t call the service linked to the RFPlayer correctly.
So I had the idea to create a generic script to execute commands via the RFPlayer.
So I created the following script:

`alias: Action Volet
sequence:
  - service: rfplayer.send_command
    data:
      command: "{{ command }}"
      protocol: "{{ protocol }}"
      device_address: "{{ device_address }}"
mode: single
`

And then, in the development tools, in the service tab, I try to call the service as follows:

service: script.action_volet
data:
  command: {% OFF %}
  protocol: {% RTS %}
  device_address: {% A1 %}


But, i always have this type of error :

2022-10-06 16:49:58.487 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Running websocket_api script
2022-10-06 16:49:58.488 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Executing step call service
2022-10-06 16:49:58.507 INFO (MainThread) [homeassistant.components.script.action_volet] Action Volet: Running script sequence
2022-10-06 16:49:58.508 INFO (MainThread) [homeassistant.components.script.action_volet] Action Volet: Executing step call service
2022-10-06 16:49:58.509 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'command' is undefined when rendering '{{ command }}'
2022-10-06 16:49:58.515 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'protocol' is undefined when rendering '{{ protocol }}'
2022-10-06 16:49:58.518 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'device_address' is undefined when rendering '{{ device_address }}'
2022-10-06 16:49:58.524 DEBUG (MainThread) [custom_components.rfplayer] Rfplayer send command for {'command': '', 'protocol': '', 'device_address': '', 'automatic_add': False}
2022-10-06 16:49:58.525 DEBUG (MainThread) [custom_components.rfplayer.rflib.rfpprotocol] writing data: b'ZIA++  \n\r'
2022-10-06 16:49:58.548 DEBUG (MainThread) [custom_components.rfplayer.rflib.rfpprotocol] received data: ZIA--Empty command
2022-10-06 16:49:58.548 DEBUG (MainThread) [custom_components.rfplayer.rflib.rfpprotocol] decoded packet: {'node': 'gateway', 'message': 'Empty command'}
2022-10-06 16:49:58.549 DEBUG (MainThread) [custom_components.rfplayer.rflib.rfpparser] f:node,v:gateway
2022-10-06 16:49:58.550 DEBUG (MainThread) [custom_components.rfplayer.rflib.rfpparser] f:message,v:Empty command

Do you have any idea of the reason why my variables are not correctly passed to the service during the service call ?

Thank you for any help.

Here’s the way I do it (as separate tap_actions and double_tap_actions that call the same script):

                          double_tap_action:
                            action: call-service
                            service: script.good_night_02
                            service_data:
                              lights_wait: '0'
                              in_bed_wait: '0'
                          tap_action:
                            action: call-service
                            service: script.good_night_02
                            service_data:
                              lights_wait: '10'
                              in_bed_wait: '15'

…and here’s the script itself:

good_night_02:
  alias: "timed lights off & in_bed indicator"
  description: "wait, turn lights off, wait, turn on in_bed_indicator"
  fields:
    lights_wait:
      description: "time in seconds to delay lights off"
      example: "30"
    in_bed_wait:
      description: "time in seconds to delay in_bed indicator"
      example: "30"
  mode: queued
  sequence:
    - service: input_select.select_option
      target:
        entity_id: input_select.good_night_phase
      data:
        option: lights
    - delay:
        seconds: "{{ lights_wait }}"
    - service: homeassistant.turn_off
      target:
        entity_id:
          - group.downstairs_lights
    - service: input_select.select_option
      target:
        entity_id: input_select.good_night_phase
      data:
        option: bed
    - delay:
        seconds: "{{ in_bed_wait }}"
    - service: homeassistant.turn_on
      target:
        entity_id:
          - input_boolean.in_bed
    - service: input_select.select_option
      target:
        entity_id: input_select.good_night_phase
      data:
        option: idle

The {% %} is used to designate a statement / control structure. A control structure refers to all those things that control the flow of a program - conditionals (i.e. if/elif/else), for-loops, as well as things like macros and blocks. On their own, as you have used them, they do not return a value. For
basic values you can just type them as-is, you do not need to wrap them in any kind of designator.

service: script.action_volet
data:
  command: OFF
  protocol: RTS
  device_address: A1

Thx Didgeridew

In fact, the solution were simpler than I imagined.

Salut depuis

repo la béta 14

En cours Beta 14 · Issue #10 · gce-electronics/HA_RFPlayer · GitHub

cette fonction est intégrée

Sinon passer sur version Beta Rfplayer

@+Dom

Top ! Merci pour le boulot. Je n’avais pas vu
Même si cela fonctionne avec le script, c’était dommage de ne pas pouvoir appeler directement.
Testé à l’instant et cela fonctionne parfaitement.