How to split this single line template?

I’m playing with a homebrew led strip controlled by an arduino - I can control it as light from HA via Template light - HA is amazingly capable!

Anyway the simple REST library I’m using on the arduino only accepts a single parameter string so I end up with this template which works fine in Developer Tools - Templates

http://192.168.1.119/switchon?params={{states("input_number.ledstrip_brightness")|int}}s{{states("input_number.ledstrip_sat")|int}}

It works and produces the desired output, e.g: http://192.168.1.119/switchon?params=71s16

How can I split it into a multiline template for ease of editing/maintenance/readability? I’ve tried various versions using > but always end up with errors.

Many thanks

Jinja supports split

Have you checked the jinja docs or tried something like this?

{% set string="Hello split at comma, please" %}

{{
string.split(",")
}}
1 Like

Thanks - the set string helped. I ended up with this:

{% set string="http://192.168.1.119/switchon?params=" %}
{% set string = string + states("input_number.ledstrip_hue")|int|string %} 
{% set string = string + "s" + states("input_number.ledstrip_sat")|int|string %}
{% set string = string + "l" + states("input_number.ledstrip_brightness")|int|string %}
{{string}}

which produces the correct output in developer tools, e.g:
http://192.168.1.119/switchon?params=279s16l71

the ‘|int|string’ is to avoid having the numbers formatted ‘279.0’ - I’m sure there’s a more elegant way but it works.

Cheers

you’re overriding the built in string filter/function. Don’t do that.

{% set a = "http://192.168.1.119/switchon?params=" %}
{% set b = states("input_number.ledstrip_hue") | int %}
{% set c = "s" ~ states("input_number.ledstrip_sat") | int %}
{% set d = "l" ~ states("input_number.ledstrip_brightness") | int %}
{{ a ~ b ~ c ~ d }}

or

{% set a = "http://192.168.1.119/switchon?params=" %}
{% set b = states("input_number.ledstrip_hue") | int %}
{% set c = states("input_number.ledstrip_sat") | int %}
{% set d = states("input_number.ledstrip_brightness") | int %}
{{ a }}{{ b }}s{{ c }}l{{ d }}

that’s assuming you even need the ints

or

{% set a = "http://192.168.1.119/switchon?params=" %}
{% set b, c, d = ["input_number.ledstrip_hue", "input_number.ledstrip_sat", "input_number.ledstrip_brightness"] | map('states') | map('int') | list %}
{{ a }}{{ b }}s{{ c }}l{{ d }}
1 Like

Yet another way to do the same thing:

{{ 'http://192.168.1.119/switchon?params={hue}s{saturation}l{brightness}'.format(
  hue = states('input_number.ledstrip_hue') | int,
  saturation = states('input_number.ledstrip_sat') | int,
  brightness = states('input_number.ledstrip_brightness') | int) }}

EDIT

Added hue.


If you prefer shorter variable names:

{{ 'http://192.168.1.119/switchon?params={h}s{s}l{b}'.format(
  h = states('input_number.ledstrip_hue') | int,
  s = states('input_number.ledstrip_sat') | int,
  b = states('input_number.ledstrip_brightness') | int) }}

Or no variable names:

{{ 'http://192.168.1.119/switchon?params={}s{}l{}'.format(
  states('input_number.ledstrip_hue') | int,
  states('input_number.ledstrip_sat') | int,
  states('input_number.ledstrip_brightness') | int) }}
1 Like

Thanks @123, @petro for all the different solutions. The choices can be overwhelming :grinning:

I’ve become aware of so many new templating capabilities which hopefully will help others searching with similar issues. .format looks powerful but ~ string concatenation seems nice and straightforward to me.

so my new solution is

{% set a = "http://192.168.1.119/switchon?params=" %}
{% set b = states("input_number.ledstrip_hue") | int %}
{% set c = "s" ~ states("input_number.ledstrip_sat") | int %}
{% set d = "l" ~ states("input_number.ledstrip_brightness") | int %}
{{ a ~ b ~ c ~ d }}

Thanks again

PS I modified my arduino code to expect floats in the params string, e.g:
http://192.168.1.119/switchon?params=240.0s100.0l74.0

which meant I could get rid of the ints.

Simple is good.

You’re welcome!

BTW, you should assign the Solution tag to petro’s post because it’s the one that contains the solution you chose to use.

Reference:
FAQ Guideline 21 - Somebody’s answer solved it