Filtering power usage isnt seen in template, how to fix?

using this rather contrived template, I am searching for power sensors that should be switched off, but are not correctly so for some reason, and still using power.

this was working just fine until the moment I decided to have my zwave sensors show their self power usage, so they’re always showing 0.5/0.8 even when off. filtering these doesnt work just yet

that is, they are filtered completely, and the or states(s.entity_id) == 0.5 bit is ignored…

please have a look what I should change to also have that incorporated? And ofc if there’s an obvious improvement please don’t hold back :wink:

thanks!

template:

  - sensor:

      - unique_id: power_using_off_switches
        name: >
          Power using 'off'-switches
        state: >
          {%- set ns = namespace(not_off=[]) %}
          {% set self_power_devices_05 =
            ['sensor.audio_gym_actueel', 'sensor.master_bedroom_google_actueel',
            'sensor.tv_tuner_badkamer_actueel'] %}
          {% set self_power_devices_08 =
            ['sensor.vijverpomp_links_actueel','sensor.vijverpomp_rechts_actueel'] %}
          {% set ignore =
            ['sensor.inductieplaat_actueel','sensor.patchboard_zolder_actueel'] %}
          {%- for s in expand('group.switches_sensors_actueel')
            if s.entity_id not in ignore and
              (s.entity_id not in self_power_devices_05 or states(s.entity_id) == 0.5)
          and (s.entity_id not in self_power_devices_08 or states(s.entity_id) == 0.8)
          and states(s.entity_id) not in ['0','0.0'] and
              states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}

          {%- set ns.not_off = ns.not_off + [s.entity_id] %}
          {%- endfor %}

           {{ns.not_off|count}}
        attributes:
          list: >
            {%- set ns = namespace(not_off=[]) %}
            {% set self_power_devices_05 =
              ['sensor.audio_gym_actueel', 'sensor.master_bedroom_google_actueel',
              'sensor.tv_tuner_badkamer_actueel'] %}
            {% set self_power_devices_08 =
              ['sensor.vijverpomp_links_actueel','sensor.vijverpomp_rechts_actueel'] %}
            {% set ignore =
              ['sensor.inductieplaat_actueel','sensor.patchboard_zolder_actueel'] %}
            {%- for s in expand('group.switches_sensors_actueel')
              if s.entity_id not in ignore and
                (s.entity_id not in self_power_devices_05 or states(s.entity_id) == 0.5)
            and (s.entity_id not in self_power_devices_08 or states(s.entity_id) == 0.8)
            and states(s.entity_id) not in ['0','0.0'] and
                states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}

            {%- set ns.not_off = ns.not_off + [s.name+ ': ' + s.state + ' watt'] %}
            {%- endfor %}

            {% set count_not_off = ns.not_off|count %}
            {% set list = ns.not_off %}
            {% if count_not_off == 0 %} All fine
            {% elif count_not_off == 1 %}
                {{list[0]}} is still using power!
            {% elif count_not_off == 2 %}
                {{list|join(' and ')}} are still using power!
              {% else %}
                {{list[:-1]|join(', ')}}, and {{list[-1]}} are still using power!
              {% endif %}

states returns a string and you’re comping it to a float

hi, (I thought I had deleted the message because I believed to have found the fix)

template:

  - sensor:

      - unique_id: power_using_off_switches
        name: >
          Power using 'off'-switches
        state: >
          {%- set ns = namespace(not_off=[]) %}
          {% set self_power_devices_05 =
            ['sensor.audio_gym_actueel', 'sensor.master_bedroom_google_actueel',
            'sensor.tv_tuner_badkamer_actueel'] %}
          {% set self_power_devices_08 =
            ['sensor.vijverpomp_links_actueel','sensor.vijverpomp_rechts_actueel'] %}
          {% set ignore =
            ['sensor.inductieplaat_actueel','sensor.patchboard_zolder_actueel'] %}
          {%- for s in expand('group.switches_sensors_actueel')
            if s.entity_id not in ignore and
              (s.entity_id not in self_power_devices_05 or states(s.entity_id) != '0.5')
          and (s.entity_id not in self_power_devices_08 or states(s.entity_id) != '0.8')
          and states(s.entity_id) not in ['0','0.0'] and
              states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}

          {%- set ns.not_off = ns.not_off + [s.entity_id] %}
          {%- endfor %}

           {{ns.not_off|count}}
        attributes:
          list: >
            {%- set ns = namespace(not_off=[]) %}
            {% set self_power_devices_05 =
              ['sensor.audio_gym_actueel', 'sensor.master_bedroom_google_actueel',
              'sensor.tv_tuner_badkamer_actueel'] %}
            {% set self_power_devices_08 =
              ['sensor.vijverpomp_links_actueel','sensor.vijverpomp_rechts_actueel'] %}
            {% set ignore =
              ['sensor.inductieplaat_actueel','sensor.patchboard_zolder_actueel'] %}
            {%- for s in expand('group.switches_sensors_actueel')
              if s.entity_id not in ignore and
                (s.entity_id not in self_power_devices_05 or states(s.entity_id) != '0.5')
            and (s.entity_id not in self_power_devices_08 or states(s.entity_id) != '0.8')
            and states(s.entity_id) not in ['0','0.0'] and
                states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}

            {%- set ns.not_off = ns.not_off + [s.name+ ': ' + s.state + ' watt'] %}
            {%- endfor %}

            {% set count_not_off = ns.not_off|count %}
            {% set list = ns.not_off %}
            {% if count_not_off == 0 %} All fine
            {% elif count_not_off == 1 %}
                {{list[0]}} is still using power!
            {% elif count_not_off == 2 %}
                {{list|join(' and ')}} are still using power!
              {% else %}
                {{list[:-1]|join(', ')}}, and {{list[-1]}} are still using power!
              {% endif %}

making that comparing 2 strings…
but that might be not as correct as you’d like it to be.

I’d better do

states(s.entity_id)|float(0) != 0.5

?

Yes, states are strings so you need to convert