Value_template from a string (for lights)

ok, so i have a long string , example :

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

16 , 22 , 23 , 25 , … are the id’s from my light
the number after it like 0 or 100 => is light off / on

how can i use this in a value_template for my template lights?

Is the number of values in the string constant?
What is the entity_id of the entity that contains the string?

well, the id’s are always the same

its the value 0 that can be changed to 100

so in example below

16 0 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 0 56 0 61 0

here are all lights off

16 100 22 100 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 0 56 0 61 0

here are lights 16 and 22 ON

16 and 22 , … are indeed always the same , ids are fixed values

Ah ok. I thought you only wanted the last number of the string. Now I understand. You want the state for each id in the string.

You could write a template for each id (using the split() method) but there’s got to be a better way. e.g.

First light state:

value_template: "{{ states.sensor.your_sensor_that_contains_the_string.split(" ")[1] }}"

Second light state:

value_template: "{{ states.sensor.your_sensor_that_contains_the_string.split(" ")[3] }}"

Third light state:

value_template: "{{ states.sensor.your_sensor_that_contains_the_string.split(" ")[5] }}"

etc…

i.e. count up split(" ")[n] in odd numbers, n = 1,3,5,7,9,11… for light in position 1,2,3,4,5,6… in the string.

1 Like

yes, that would work

so if it gets 100 , it will be ON, it gets 0 , it will be OFF

good

Not quite. That only gets the state (0 or 100). To convert the state to on/off for a light template the full template would be:

value_template: "{{ states.sensor.your_sensor_that_contains_the_string.split(" ")[1] | int == 100 }}"

thnx , but i have a parse error on that one :frowning:

2019-05-17 14:05:06 ERROR (SyncWorker_2) [homeassistant.util.yaml] while parsing a block mapping
in “/config/lights.yaml”, line 4, column 7
expected , but found ‘’
in “/config/lights.yaml”, line 6, column 54

command :

value_template: "{{ states.sensor.niko.split(" ")[1] | int == 100 }}"

seems this doesnt work i think : split(" ")

if i changed it to split( ), then no parse error, but not working either offcourse
maybe single quote

Its a quote thing. The first quote outside the whole template matches with the first quote in split("…

You end up with this string: "{{ states.sensor.niko.split("

Try using single quotes outside the template:

value_template: '{{ states.sensor.niko.split(" ")[1] | int == 100 }}'

ok, gonna try it
it doesnt have to be : states.sensor.niko.state.split

the missing .state before split?

well, no parse error, but an error in log :frowning:
2019-05-17 14:13:29 ERROR (MainThread) [homeassistant.helpers.entity] Update for light.keukeneiland fails
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 220, in async_update_ha_state
await self.async_device_update()
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 375, in async_device_update
await self.async_update()
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/template/light.py”, line 237, in async_update
if state in _VALID_STATES:

Yeah it does.

I was using this in the template editor to check and missed that, sorry.

{% set string = '1 2 3 4 5' %}
{{ string.split(" ")[1] }}

So:

value_template: '{{ states.sensor.niko.state.split(" ")[1] | int == 100 }}'

Should work.

should be indeed, but i see this error in log :

  lights:
    keukeneiland:
      friendly_name: "Keuken Eiland"
      value_template: '{{ states.sensor.niko.state.split(" ")[1] | int == 100 }}'
      turn_on:
        - service: switch.turn_on
          data: 
            entity_id: switch.light5

      turn_off:
        - service: switch.turn_off
          data: 
            entity_id: switch.light5

sensor : (so first light to test id=16)
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

2019-05-17 14:17:34 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform kostal is taking over 10 seconds.
2019-05-17 14:17:44 ERROR (MainThread) [homeassistant.components.template.light] UndefinedError: list object has no element 1
2019-05-17 14:17:44 ERROR (MainThread) [homeassistant.helpers.entity] Update for light.keukeneiland fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 375, in async_device_update
    await self.async_update()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/template/light.py", line 237, in async_update
    if state in _VALID_STATES:
UnboundLocalError: local variable 'state' referenced before assignment

What do you get when you paste this into the dev tools template editor:

 '{{ states.sensor.niko.state.split(" ")[1] | int == 100 }}'

Error rendering template: UndefinedError: list object has no element 1

Errr. Okay what about this:

{{ states.sensor.niko.state }}

aha, ok, thats the issue, its a string like this , seems a “return” is the split character