Value_template from a string (for lights)

Yep.

I’ve never seen it used here, but try splitline()[n] instead of split(" ")[n]

this : ‘{{ states.sensor.niko.state.splitline()[1] | int == 100 }}’
gives:
Error rendering template: UndefinedError: ‘str object’ has no attribute ‘splitline’

Help is at hand. Wait for it…

this should work

{% set light = '16' %}
{% set lights = '16 100 22 0 23 0 25 0 55 0 33 0 34 0 40 0 42 0 43 0 44 0 49 0 50 100 51 0 52 0 54 100 56 0 61 0' %}
{%- macro getlight(name, instring) %}
{%- set values = instring.splitlines() %}
{%- for i in range(0, values | length, 2) %}
{%- if name == values[i] %}
{{ values[i+1] }}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{%- set brightness = getlight(light, lights).strip() %}
{%- if brightness != '' %}
{{ 'on' if brightness == '100' else 'off' }}
{%- else %}
undefined
{%- endif %}

Just replace lights with wherever you’re getting the list. And change the number ‘16’ to whatever id light you want.

omg :slight_smile:

well , seems this also worked, ‘{{ states.sensor.niko.state.splitlines()[1] | int == 100 }}’

you forgot the ‘s’ of splitlines :slight_smile:
gonna try your code also later @petro
thnx so much guys

1 Like

Trouble is the list is not separated by spaces but carriage returns or newlines.

Say what? who did that.

my netcat command did :slight_smile:

carriage returns are \n or \r\n depending on the language

ok, splitlines ,is working

thnx!!! :slight_smile:
really appreciate it

splitlines() does both cases I think, which is why I went for it.

I don’t know to be honest. It may try all the possible combinations.

I updated the code I wrote to handle the carriage returns.

Ok, thbx Petro :wink:

If there was a contest to produce an awkward reporting protocol, this device would win it.

What kind of lighting device is it?

Niko home control :wink: