Take out a part of a string , and do an action

hi, i have a sensror, thats reading a hex string
for example
01000100…
every light in my house is 2 bits long, so this means (in example above)
light 1 : 01 (on)
ligth 2 : 00 (off
ligth 3 : 01 (on)
light 4 : 00 (off)

so i have an 4 automation configured:
automation 1

      - condition: template
value_template: "{{ is_state('sensor.hex', '01000000') }}"

but this is not working ffcourse , because i read the whole line

how can i only read the first 2 values, and ignore the rest
for automation 2 , i only need to read 3 th and 4th character and igore the rest

how can i read that ?

sensor.hex AND FF000000 for light 1, sensor.hex AND 00FF0000 for light 2 etc.

1 Like

I don’t think you can use is_state, which is too bad, but this should work:

First automation:
value_template: "{{ states.sensor.hex.state[0:2] == '01') }}"

Second automation:
value_template: "{{ states.sensor.hex.state[2:4] == '01') }}"

and so on.

If you use the state of the lights in several places, perhaps make template sensors for each light and use them in the automations.

cool, i can use that :slight_smile: , thnx for fast answers

indeed, i can make multiple sensors, but in programming time it takes the same amount of time
i have a domotica system, with no integrations or bindings whats so ever , but i cand send and read out TCP commands, in hex format, based on a 48 bit long string i know the output of my lights, etc …
so i have one sensor to read out my whole home, then based on automation, i am going to change the state of a switch
because it can happen offcourse, it we put the light on on a classic switch outside of HA, then offcourse HA needs to change the state also, but without actually turning it off/on

thnx!!

btw, isnt that wrong? shouldt it be : state[0:1] , so only first 2 digits

1 Like

You are new to ha by the sound of it. In ha this is a ‘light’ not a ‘sensor’. And each light is a single entity, although you can group a number of lights if you want to have them all act together.

ok, thnx, well i just test t :slight_smile:

The slice notation is the same as pythons (I think), so [0:2] returns indexes 0 to 1, and [2:4] returns indexes 2 to 3.

Try it in the Templates editor: {{ '0123456'[0:2] }} returns 01

The first digit is the first index to include and the second is the first index to NOT include. Sounds strange, but works well.

2 Likes

Ok, thnx for the explanation

hmm, i tried your line in my automation

    value_template: "{{ states.sensor.Prog.state[0:2] == '01') }}"

gives an error :

nvalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘condition’][0][‘value_template’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘condition’]. Got None
required key not provided @ data[‘condition’][0][‘entity_id’]. Got None.

if i change it , how i did it before, on my old config, just reading sensor by sensort
then i dont have an program script error, but it doesnt work either, doesnt start the automation

value_template: "{{ is_state('sensor.Prog[0:2]', '01') }}"

Remove the ) after ‘01’

"{{ states.sensor.Prog.state[0:2] == '01' }}"

1 Like

omg, didnt notice !! just copy pasted it from above

thnx!