I want to set up an input slider who’s value is used in an MQTT message.
Slider will have 0-255 positions
And this need to control the last 2 characters of the payload
Payload needs to be 000000XX where XX is a value from 00 - FF taken from the slider.
I can set up the slider and can send the mqtt message with a fixed payload.
Can anyone help to link the slider to the payload in the correct format.
Thanks for the info.
Tried using the template tool but can’t create a valid script.
Can anyone offer more detail.
tom_l
October 14, 2018, 10:35pm
4
You dont put scripts in the template tool. It’s for evaluating templates only.
If you put this in the template editor what do you get as the result:
{% set value = states.input_number.your_input_number.state | int %}
{{ "%+0x" | format( value | int }}
Think you are missing a ) after int in 5he second line.
This give me the hex for the slider value with a leading + (ie +FF)
How can I amend this to give 6 leading 0s insted of + (ie 000000FF)
This could then be used to pass to the mqtt message.
tom_l
October 15, 2018, 12:37am
6
Oops, yes sorry about the missing bracket. There’s probably an easier way to do it but this should format it how you want
{% set value = states.input_number.your_input_number.state | int %}
{{ "%+0x" | format( value ) | int }}
{{ value | replace("+", "000000") }}
) is still in the wrong place need to be after the int or the result is 0
{% set value = states.input_number.your_input_number.state | int %}
{{ "%+0x" | format( value | int )}}
The last line
{{ value | replace("+", "000000") }}
does not work the returned result is back to 255. ( or whatever the original value was)
Wandering if value needs converting to a string first before doing the replace but don’t know if this is right or how to do it.
Help so far is great while i try to learn the basics of templates.
tom_l
October 15, 2018, 8:34am
8
Hopefully this should work:
payload template: "000000{{ "%0x" | format( states.input_number.your_input_number.state | int ) | string }}"
Using this give me an error when checking the config file.
Configuration invalid CHECK CONFIG
Error loading /config/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token in “/config/automations.yaml”, line 1173, column 36
Sorry for all the questions but definitely a noob when it comes to value templates and trying to learn by example
I did spot that one.
Tried both ways but configuration check still give the same error.
Tried running this in the templates dev tool and it does output as i need.
Just can’t make it work in the payload_template
Solved it
I have changed the double quotation marks around the %0x to singles and it now works
working line reads as
payload template: "000000{{ '%0x' | format( states.input_number.your_input_number.state | int ) | string }}"
Thanks for the help to get me to this point.
tom_l
October 16, 2018, 9:00am
14
Well done. Glad you got it sorted. Yeah sorry, that’s a rule I forgot to follow (single quotes inside double quotes).