Use Global Variables with script in ESPHome

Hello,

Im trying to run a script and use a global var as interval for delay i did something like this:

globals:
  - id: interval
    type: int
    restore_value: no
    initial_value: '5'  

switch:
  - platform: template
    name: "5 Minutes Round"
    turn_on_action:
      - lambda: |-
          interval = 5;
      script.execute: script_To_Watter_for_min;
    turn_off_action:
      if:
        condition:
         - script.is_running: script_To_Watter_for_min
        then:
         - script.stop: script_To_Watter_for_min
script:
  - id: script_To_Watter_for_min
  
    then:
      - switch.turn_on: sprinkler2
      - delay: (interval)min
      - switch.turn_off: sprinkler5
      - delay: (interval)min
      - switch.turn_off: sprinkler3
      - delay: (interval)min
      - switch.turn_off: sprinkler6
      - delay: (interval)min
      - switch.turn_off: sprinkler1
      - delay: (interval)min
      - switch.turn_off: sprinkler4

I have 2 questions how do i set the var to new value in the switch and how i should use the var as delay

You need to use lambdas in your delay and you need to use id(interval) in both accessing and setting the global variable.

Edit: check both links even though they are titled the same.

Thanks,
Ended up writing :

switch:
  - platform: template
    name: "5 Minutes Round"
    inverted: True
    lambda: !lambda |-
      return (id(interval) = 5);
    turn_on_action:
      script.execute: script_To_Watter_for_min
    turn_off_action:
      if:
        condition:
         - script.is_running: script_To_Watter_for_min
        then:
         - script.stop: script_To_Watter_for_min
         
  - platform: template
    name: "10 Minutes Round"
    inverted: True
    lambda: !lambda |-
      return (id(interval) = 10);
    turn_on_action:
      script.execute: script_To_Watter_for_min
    turn_off_action:
      if:
        condition:
         - script.is_running: script_To_Watter_for_min
        then:
         - script.stop: script_To_Watter_for_min
script:
  - id: script_To_Watter_for_min  
    then:
      - switch.turn_on: sprinkler2
      - delay: !lambda "return id(interval)*60*1000;"
      - switch.turn_off: sprinkler5
      - delay: !lambda "return id(interval)*60*1000;"
      - switch.turn_off: sprinkler3
      - delay: !lambda "return id(interval)*60*1000;"
      - switch.turn_off: sprinkler6
      - delay: !lambda "return id(interval)*60*1000;"
      - switch.turn_off: sprinkler1
      - delay: !lambda "return id(interval)*60*1000;"
      - switch.turn_off: sprinkler4

But its not doing anything when i push the switch. Any Ideea ?

I would think you should set the global variable in the turn_on_action.

I’m confused on the intent of all this however. The state of your switches can only be true or false. It seems like this would all function better as a user defined service for which you can pass a parameter.

Yes Native is a much better option , one question how do i stop a User-defined Service i couldn’t find anything

Good point. I think it all comes down to the intent of what you are trying to achieve, which isn’t clear.

So i have 6 sprinklers , i want to make an automation to run each one of them for 5 or 10 minutes . Due to logistical issues i decided to go with your suggestion and build it inside esphome via native api . So i created a service that runs them one by one with an interval param. This is working fine but i would like to be able to also stop the service if i want . This is the api:

api:
  password: "bla"
  services:
    - service: startwattering
      variables:
        interval: int
      then:
      - switch.turn_on: sprinkler2
      - delay: !lambda 'return interval;'
      - switch.turn_on: sprinkler5
      - delay: !lambda 'return interval;'
      - switch.turn_on: sprinkler3
      - delay: !lambda 'return interval;'
      - switch.turn_on: sprinkler6
      - delay: !lambda 'return interval;'
      - switch.turn_on: sprinkler1
      - delay: !lambda 'return interval;'
      - switch.turn_on: sprinkler4
      - delay: !lambda 'return interval;'
      - switch.turn_off: sprinkler4

I call it from a card in HA like this :slight_smile:

- type: button
    tap_action:
      action: call-service
      service: esphome.sprinklers_startwattering
      service_data:
        interval: 300000  
    show_icon: true
    show_name: true
    icon: 'mdi:sprinkler-variant'
    icon_height: 50px
    name: 5 Minute Round

Unfortunately, I think you need to create a script for which you can check if it is running and stop it. I would personally create separate scripts for different time intervals which makes things much easier. You could consider adding a feature request for adding passing variables to scripts in esphome, I if it isn’t requested already.

Thatbis: You could still use the service to call it from home assistant, but the service first stops all running scripts than runs your script. Unfortunately, if you really want one script, you’ll still need global variables.

But how i can check service state in esphome an also how do i stop a service