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
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?