How to pass variables from entity-button to script?

Hi everyone,

I’m trying to add a button that turns on a water pump for a series of seconds and then turns it off. For that, I have created a script like this:

water_plants_configurable:
  alias: Water plants
  sequence:
  - data:
      entity_id: switch.water_pump
    service: switch.turn_on
  - delay:
      seconds: '{{secs}}'
  - data:
      entity_id: switch.water_pump
    service: switch.turn_off

Which I’m trying to call from a Lovelace entity-button like this:

type: entity-button
name: 30 seconds
tap_action:
  action: call-service
  service: script.turn_on
  data:
    secs: 5
show_icon: true
icon: 'mdi:water'
entity: script.water_plants_configurable

However, the pump is not turned off after 5 seconds and I get this error in the logs:

Error rendering 'Water plants' delay template: expected int for dictionary value @ data['seconds']

What am I doing wrong? If I hardcode the delay in the script it works.

Thanks for your help!

Change your button to this

type: entity-button
name: 30 seconds
tap_action:
  action: call-service
  service: script.water_plants_configurable
  data:
    secs: 5
show_icon: true
icon: 'mdi:water'
entity: script.water_plants_configurable

change your script to this:

water_plants_configurable:
  alias: Water plants
  sequence:
  - data:
      entity_id: switch.water_pump
    service: switch.turn_on
  - delay: >-
      {%- set hours = secs // 3600 %}
      {%- set minutes = (secs % 3600) // 60 %}
      {%- set seconds = secs % 60 %}
      {{ [hours, minutes, seconds] | join(':') }}
  - data:
      entity_id: switch.water_pump
    service: switch.turn_off

Wow, thanks for your reply!

Unfortunately, the same error keeps being logged:

Error rendering 'Regar configurable' delay template: UndefinedError: 'secs' is undefined

The solution is replacing data with service_data in the button template:

type: entity-button
name: 30 seconds
tap_action:
  action: call-service
  service: script.water_plants_configurable
  data:
    secs: 5
show_icon: true
icon: 'mdi:water'
entity: script.water_plants_configurable

Thanks!

ah typo!

type: entity-button
name: 30 seconds
tap_action:
  action: call-service
  service: script.water_plants_configurable
  service_data:
    secs: 5
show_icon: true
icon: 'mdi:water'
entity: script.water_plants_configurable
1 Like