SOLVED: How do I best make 4 states of two sensors?

Hi All

I was thinking that I have a two bit binary number given each bit from each sensor.
0 - closed
1 - left open
2 - right open
3 - both open

So 1 sensor gives 1 to the sum, the other gives two.
I can then select the image as a ‘image0.png’ to ‘image3.png’.

But how do I make this the best wat, via a script? automations?
I haven’t touched scripts yet, but it sounds like a good occasion for it :slight_smile:

I’ll of course post the result when it’s done.

Sounds like you want a https://www.home-assistant.io/components/template/

Yes, I could do a lot of if else, but it seems like I should be able to do something like

result = 0
if sensor1 = on then result +1
if sensor2 = on then result +2
return result

I’m just not sure how to do that i HA.

Something like this is what I thought would be elegant:

    value_template: >-
      {{ is_state('binary_sensor.left', 'on') + ( is_state('binary_sensor.right','on') *2 ) }}

This is of course only pseudo setup, as I can’t figure out how it’s done properly

If I understand correctly, this is what you want to do with a template sensor…

sensor:
  - platform: template 
    sensors:
      sensor_image:
        entity_id:
          - binary_sensor.left 
          - binary_sensor.right
        value_template: >-
          {% set left = states.binary_sensor.left.state %}
          {% set right = states.binary_sensor.right.state %}
          {% if left == "off" and right == "off" %}
             {% set n = "0" %}
          {% elif left == "on" and right == "off" %}
             {% set n == "1" %}
          {% elif left == "off" and right == "on" %}
             {% set n == "2" %}   
          {% else %}
             {% set n == "3" %}
          {% endif %}
          {{ ["image", n, ".png"] | join("") }}

Hej @Jer78

That looks nice, but I get an error with it:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'string') for dictionary value @ data['sensors']['sensor_image']['value_template']. Got '{% set left = states.binary_sensor.left.state %} {% set right = states.binary_sensor.right.state %} {% if is_state(\'left\', \'off\') and is_state\'right\', \'off\' %}\n {% set n = "0" %}\n{% elif left == "on" and right == "off" %}\n {% set n == "1" %}\n{% elif left == "off" and right == "on" %}\n {% set n == "2" %} \n{% else %}\n {% set n == "3" %}\n{% endif %} {{ ["image", n, ".png"] | join("") }}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

As I can see it, it’s the ‘set’ lines that it complains about?

I have never thought of jinja as particularly elegant so that wish may be dashed on the rocky shores of templating.

Your solution though does look appealing, but may be a victim of typing - a boolean is not an integer is not text.

Ah I had an extra = sign in a few places. This is what happens when I write code at night and don’t test. Try this

sensor:
  - platform: template 
    sensors:
      sensor_image:
        entity_id:
          - binary_sensor.left 
          - binary_sensor.right
        value_template: >-
          {% set left = states.binary_sensor.left.state %}
          {% set right = states.binary_sensor.right.state %}
          {% if left == "off" and right == "off" %}
             {% set n = "0" %}
          {% elif left == "on" and right == "off" %}
             {% set n = "1" %}
          {% elif left == "off" and right == "on" %}
             {% set n = "2" %}   
          {% else %}
             {% set n = "3" %}
          {% endif %}
          {{ ["image", n, ".png"] | join("") }}
1 Like

No, that’s what high level programming languages gets you, lazy programmers and automatic type conversion :smiley:

Ha ha, yes, that is never a good idea :smiley:
It accepts it now, but it sets it to ‘3’ no matter what.

Are you sure the payload data for the sensors is “on” and “off”? If not, that would explain why none of the conditions are met and the result is always set to 3.

1 Like

I think I found out why, the state is set to ON and not on etc.
Unfortunately I have to wait to test it out until I get to my computer again, I’m currently volunteering as an official in a 306 km amateur cycle race called Sjælland Rundt :grin:

- platform: mqtt
  name: 'Entré dør'
  state_topic: 'tele/rfbridge/RESULT'
  value_template: >-
    {% if value_json.RfReceived.Data == '12345A' %}
      {{'ON'}}
    {% elif value_json.RfReceived.Data == '12345E' %}
      {{'OFF'}}
    {% else %}
      {{states('binary_sensor.entre_dor') | upper}}
    {% endif %}
  device_class: door

This would become very complex if you would add more sensors in the future.

You might have a look at

and search for ‘bitwise_and’ or ‘bitwise_or’

1 Like

For this, I won’t have more sensors.

If you are referring to the mqtt sensor, then it’s the way it is, until I get the de multiplexing setup for the rfbridge.

What I see are two binary_sensors that serve as ‘bits’ in a binary number. So you only need to use arithmetic to calculate the value they represent.


sensor:
  - platform: template 
    sensors:
      sensor_image:
        entity_id:
          - binary_sensor.left 
          - binary_sensor.right
        value_template: >-
          {% set l = 1 if states('binary_sensor.left') == 'on' else 0 %}
          {% set r = 1 if states('binary_sensor.right') == 'on' else 0 %}
          {% set n = (r * 2) + l %}
          image{{n}}.png

Or simply combine the last two lines into one:

          image{{(r * 2) + l}}.png
2 Likes

Not sure what changed, I recopied your suggestion, and now it shows it correctly in the sensor.

Yes, exactly, that was my thought as well, and also what I tried to illustrate in the pseudo code I wrote.
Oh, that looks very nice the way you did that, and I think I can see how to show the image as well :slight_smile:

Hi @123
That sensor setup worked perferctly, and it is very elegant.

1 Like

The result can be seen here:

Next step is temperatures etc.
I haven’t figure out how to use the sensor value for the picture, so for now, I’ve just changed it to 0-3 as value, and then a state picture in the card.