Outputs look equal but IF statement says FALSE

Hoping someone can help figure out why my two outputs in this test appear to be the same but my “if” statement says otherwise. I am pretty sure it has something to do with how my variables present but I am too much of a noob to figure it out. Thanks.

- alias: Samuel's Ceiling Fan States Check
    {% set entity = expand('binary_sensor.samuel_s_ceiling_fan_wattage') | selectattr('state', 'eq', 'on') |map(attribute='entity_id')|join(', ') %}
    {% set fan_current_state = states('switch.fan_light_samuel_s_room'),states('counter.samuel_fan_light'),state_attr('fan.samuel_fan','preset_mode'),states('counter.samuel_fan_speed') %}

    {% set d = { 'binary_sensor.samuel_fan_low_speed':('off','0','low','1'),
            'binary_sensor.samuel_fan_light':('on','1','off','0'),
            'binary_sensor.samuel_fan_medium_speed':('off','0','medium','2'),
            'binary_sensor.samuel_fan_low_speed_light':('on','1','low','1'),
            'binary_sensor.samuel_fan_medium_speed_light':('on','1','medium','2'),
            'binary_sensor.samuel_fan_high_speed':('off','0','high','3'),
            'binary_sensor.samuel_fan_high_speed_light':('on','1','high','3'),
            'binary_sensor.samuel_fan_off': ('off','0','off','0')
          }
        %}
      {% set fan_true_state = d.items() | selectattr('0','in',[entity]) | map(attribute='1') | join(',') %}
      {{entity}}
      {{fan_current_state}}
      {{fan_true_state}}

      {% if fan_current_state == fan_true_state %}
          true
      {% else %}
          false
      {% endif %}

image

fan_current_state is an actual tuple, but fan_true_state is a tuple-shaped string.

So what you are saying is somehow I am typing my own language? Any easy way to fix this?

Probably, but I’m a bit confused about what you are actually trying to do with a couple parts of your template, especially the following:

{% set entity = expand('binary_sensor.samuel_s_ceiling_fan_wattage') 
| selectattr('state', 'eq', 'on') | map(attribute='entity_id') | join(', ') %}
{% set fan_true_state = 
d.items() | selectattr('0','in', [entity]) | map(attribute='1') | join(',') %}

As you probably can tell this is just a test template. What I am trying to do is compare RF states on a ceiling fan. I know the actual states based on threshold binary sensors which you see in variable “d”. These threshold sensor readings come from a watt reading on the Shelly 2.5 I have installed in the wall switch of the fan. This would be the “fan_true_state” and I want to compare this to the “fan_current_state” and if the two don’t match I can then correct the states in HA.

{% set entity = expand('binary_sensor.samuel_s_ceiling_fan_wattage') 
| selectattr('state', 'eq', 'on') | map(attribute='entity_id') | join(', ') %}

So what this statement is doing is extracting from group ‘binary_sensor.samuel_s_ceiling_fan_wattage’ the actual threshold sensor that is on and this will give me a reference for which true state to use.
i.e. “binary_sensor.samuel_fan_light’:(‘on’,‘1’,‘off’,‘0’)”

{% set fan_true_state = 
d.items() | selectattr('0','in', [entity]) | map(attribute='1') | join(',') %}

This statement is then taking that string and stripping out the entity_id and leaving me with the states “(‘on’,‘1’,‘off’,‘0’)” for comparison to “fan_current_state”

If I’m understanding you, the following should work…

{% set entity = state_attr('binary_sensor.samuel_s_ceiling_fan_wattage', 'entity_id')
| select('is_state', 'on') | first ) 

{% set fan_current_state = [ 
states('switch.fan_light_samuel_s_room'), 
states('counter.samuel_fan_light'), 
state_attr('fan.samuel_fan','preset_mode'), 
states('counter.samuel_fan_speed') ] %}

{% set d = { 'binary_sensor.samuel_fan_low_speed': ['off', '0', 'low', '1'],
      'binary_sensor.samuel_fan_light': ['on', '1', 'off', '0'],
      'binary_sensor.samuel_fan_medium_speed': ['off', '0', 'medium', '2'],
      'binary_sensor.samuel_fan_low_speed_light': ['on', '1', 'low', '1'],
      'binary_sensor.samuel_fan_medium_speed_light': ['on', '1', 'medium', '2'],
      'binary_sensor.samuel_fan_high_speed': ['off', '0', 'high', '3'],
      'binary_sensor.samuel_fan_high_speed_light': ['on', '1', 'high', '3'],
      'binary_sensor.samuel_fan_off': ['off', '0', 'off', '0']
} %}

{% set fan_true_state = d.get(entity) %}
{{ fan_current_state == fan_true_state }}

This is working. Thank you so much!!!

1 Like