Noob question: how to multiply a variable in a script?

Hi all,

I have a blind with tilting slats. In order to tilt the slats, I need to use the closing movement during a more or less long time - from 12 ms (almost horizontal slats) to 1200 ms (completely closed slats).

I want to use a script to tilt the slats to a specific percentage which I pass as a variable. However I can’t find how to multiply this percentage (expressed as a number from 0 to 100) by a static value so that I can use it as a delay in milliseconds.

I found a million examples that use a sensor value, but I don’t have a sensor, only the variable…

Here is the code snippet which I’m trying to get to work:

#script
# receives the cover to tilt {{ cover_to_tilt }} and the tilt target value {{ tilt }} between 0 and 100
cover_tilt:
  sequence:
  - service: cover.close_cover
    target:
      entity_id: '{{ cover_to_tilt }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      # here I need to calculate 12 x the percentage received as {{ tilt }}
      # milliseconds: '{{ tilt }} * 12'
      milliseconds: ?????
  - service: cover.stop_cover
    target:
      entity_id: '{{cover_to_tilt }}'

Thank you for your help!

You need to have the multiplier inside the template expression:

#script
# receives the cover to tilt {{ cover_to_tilt }} and the tilt target value {{ tilt }} between 0 and 100
cover_tilt:
  sequence:
    - service: cover.close_cover
      target:
        entity_id: "{{ cover_to_tilt }}"
    - delay:
        hours: 0
        minutes: 0
        seconds: 0
        milliseconds: "{{ tilt * 12 }}"
    - service: cover.stop_cover
      target:
        entity_id: "{{cover_to_tilt }}"

Note that the docs state that milliseconds will not be exact…

# When using milliseconds, consider that delay as at least X milliseconds. It won´t be exact.

1 Like

Thank you. I had already tried that but something must be off because it’s supposed to stop after 600 milliseconds when tilt = 50, but it doesn’t stop… Is there a way to display or log the value of tilt to make sure that it’s 50?

Send it as a notification to you or look at the trace

1 Like

Is there a value for which it does work? How are you passing the tilt variable?

Thanks for the suggestions. Indeed I displayed the value in a notification, and it’s empty…

I am following the official doc which states:

In my mind this means that I need to define a ‘function’ set_cover_tilt_position in my template cover, and it will receive automagically this ‘tilt’ variable.

I defined my cover template as follows:

- platform: template
  covers:
    volet_tv:
      friendly_name: "Volet TV"
      unique_id: cover_volet_tv
      device_class: shutter
      open_cover:
        service: script.volet_move
        data:
          switch_to_stop: "switch.mq_k1_volet_tv_down"
          switch_to_start: "switch.mq_k1_volet_tv_up"
          timer: "timer.volet_tv"
      close_cover:
        service: script.volet_move
        data:
          switch_to_stop: "switch.mq_k1_volet_tv_up"
          switch_to_start: "switch.mq_k1_volet_tv_down"
          timer: "timer.volet_tv"
      stop_cover:
        service: script.volet_stop
        data:
          switch_up: "switch.mq_k1_volet_tv_up"
          switch_down: "switch.mq_k1_volet_tv_down"
          timer: "timer.volet_tv"
      # tilt attempt
      set_cover_tilt_position:
        service: script.volet_set_tilt_position
        data: 
          # tilt is supposed to be passed automatically
          volet_to_tilt: "cover.volet_tv"

When I try to display it it’s empty though… What am I missing?

Isn’t that a service call?
Just like your cover.close you used before?
I don’t have any covers so I can’t say for sure but it seems to me as a service call.

1 Like

Where is that indicated? The variable tilt it is talking about is generated automatically with scope local to the configuration variable. You should pass it explicitly if you are going to use a script as your action for the service call.

1 Like

Thanks, this was the root cause. I assumed that the variable was passed automatically, but it needed to be explicitely passed as a parameter.

Thank you all!