Templates and number inputs?

The following code works fine inside an Entities Card Configuration, when sending either of two payloads to an device over MQTT:

type: entities
entities:
  - entity: input_number.sprinkler1_time
  - entity: input_boolean.sprinkler_1
    hold_action:
      action: call-service
      service: mqtt.publish
      service_data:
        payload: 1
        topic: /os/gadget/12678832/1/0
    icon: 'mdi:sprinkler'
    icon_height: 24px
    name: Sprinkler 1
    show_icon: true
    show_name: true
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        payload: 1
        topic: /os/gadget/12678832/1/1
    type: entity-button
show_header_toggle: false

So works but this is despite the code editor reporting a warning, being:

Expected a value of type `{entity,name,icon} | entity-id` for `entities.1.hold_action` but received `{"action":"call-service","service":"mqtt.publish","service_data":{"payload":1,"topic":"/os/gadget/12678832/1/0"}}`.

So, not sure how to deal with that warning since the code works both in hold_action and tap_action. The tap_action sending the 1 to the widget over MQTT and the widget then interprets that as request for a fixed 15 minute time. The hold action stopping the timed widget.

I am, however, now trying to set a time at the widget in milliseconds between 5 and 15 minutes using the input_number widget called sprinkler1_time, which is the first entity in the configuration. The definition of sprinkler1_time being:

input_number:
  sprinkler1_time:
    name: "Sprinkler 1 time"
    icon: mdi:clock-start
    initial: 5
    min: 5
    max: 15
    step: 1

So the code below was where I got to using the template help page (converting 5…15 to minutes in milliseconds):

type: entities
entities:
  - entity: input_number.sprinkler1_time
  - entity: input_boolean.sprinkler_1
    hold_action:
      action: call-service
      service: mqtt.publish
      service_data:
        payload: 1
        topic: /os/gadget/12678832/1/0
    icon: 'mdi:sprinkler'
    icon_height: 24px
    name: Sprinkler 1
    show_icon: true
    show_name: true
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        payload: "{{ states('input_number.sprinkler1_time') | int * 60000 }}"
        topic: /os/gadget/12678832/1/1
    type: entity-button
show_header_toggle: false

The device now does not follow the buttons, suggesting either the value is not sent or a value the widget receives is binned because it is not recognised (I put alot of defensive code into that sucker!).

Going into the card editor now sees two things. First the same warning as above.
Secondly going back into the editor finds the editor appears to have converted the template from:

"{{ states('input_number.sprinkler1_time') | int * 60000 }}"

to:

'{{ states(' 'input_number.sprinkler1_time' ') | int * 60000 }}'

Kooky, but if that’s what floats the card editor’s boat :crazy_face:

When I hook into the topics via node-red I am receiving the following string in the node-red debug console:

"{{ states('input_number.sprinkler1_time') | int * 60000 }}"

Instead of the calculated value of between 300000…900000.

If I lift the quotes of each end of the template thus:

{{ states('input_number.sprinkler1_time') | int * 60000 }}

I get an error flash up when I use the tap_action:

Failed to call service mqtt/publish. payload must be a string, bytearray, int, float or None

I tried the following from the MQTT Publish help:

payload_template: {{ states('input_number.sprinkler1_time') | int * 60000 }}

I save the editor and test but get the following error on tap_action:

Failed to call service mqtt/publish. value should be a string for dictionary value @ data['payload_template']

Turns out, when I go back into the editor the editor has changed:

        payload_template: {{ states('input_number.sprinkler1_time') | int * 60000 }}

to:

        payload_template:
          '[object Object]': null

Of course, using the following:

payload_template: "{{ states('input_number.sprinkler1_time') | int * 60000 }}"

causes:

Failed to call service mqtt/publish. extra keys not allowed @ data['payload_template:payload_template']

The following just causes the editor to report errors:

payload_template: '{{ states('input_number.sprinkler1_time') | int * 60000 }}'

So, I appear not to be using the template correctly?

Any hints please?

I believe (based on this thread) you would need to use a custom card to support templating.

In an entities card, you can do this:

    tap_action:
      action: call-service
      service: script.sprinkler1

then create a script that performs the actual publishing of the templated payload to /os/gadget/12678832/1/1.

  sprinkler1:
    alias: 'Sprinkler 1'
    sequence:
      service: mqtt.publish
      data_template:
        topic: /os/gadget/12678832/1/0
        payload: "{{ states('input_number.sprinkler1_time') | int * 60000 }}"
1 Like

The warnings in the GUI are broken. Ignore them. There’s an issue already on the github about this.

You can’t template inside lovelace.

@123 Thanks for the explanation! I will try that tomorrow night.

So, @123, some pain still.

I created a script with “Name” sprinkler1_script via the editor, with a null data field from the editor.

Of course, the tap_action was then written as:

tap_action:
      action: call-service
      service: script.sprinkler1_script

So when I ran the tap_action that would return a failure to find the script “sprinkler1_script”. A bit of poking around determined that the editor parameter “Name” is actually the script parameter “alias” which is, of course, not intuitive as the script turns up in the scripts.yaml as:

'1595033396620':
  alias: sprinkler1_script
  sequence:
  - data: {}
    service: mqtt.publish

So when using sprinkler1_script in the call, HA reports no script of that name but using 1595033396620 calls the script.

Now starting with the tap_action as:

tap_action:
      action: call-service
      service: script.1595033396620

That now fails with:

Failed to call service script/1595033396620. required key not provided @ data['topic'

But that’s okay since that’s because I have used the default data field and now that I have the script called I can move onto try to pump the input_number value into the payload as you have suggested.

Thanks again @123 for pointing me to the scripts.

It’s the custom of this community to mark the first post that leads to, or supplies, the solution with the Solution tag. The solution to your problem was you can’t template in a standard card an you should change the action to call-service and service to the name of a script. I offered you that in my first post, and included a suggested script.

The ‘pain’ caused by using the Script Editor is an implementation detail specific to the use of the Script Editor. That’s simply how it assigns unique names to scripts and is secondary to what constitutes the actual solution.

@123 swapped out to your first suggestion then. Though I think you might agree, it’s really between your seed and my elaboration the whole picture is captured :nerd_face:

PS, also in HA user group tradition, I will leave the question of whether I finally got the thing going up in the air :smiling_imp:, noting that it turns out 1) I did not need use a custom card and 2) I did use lovelace to create the script, so the idea that you “cannot template in lovelace” and still build a templated script in lovelace seems contradictory or at least incongruous.

1 Like