Get state from sensors

Hi, I need to get the status of some sensors that have keys.

      - name: "Fault 1"
        class: ""
        state_class: ""
        uom: ""
        scale: 1
        rule: 1
        registers: [ 0x0405 ]
        isstr: true
        icon: 'mdi:wrench'
        lookup:
          - key: 0
            value: "No error"
          - key: 1
            value: "ID01 Grid Over Voltage Protection"
          - key: 2
            value: "ID02 Grid Under Voltage Protection"
          - key: 4
            value: "ID03 Grid Over Frequency Protection"
          - key: 8
            value: "ID04 Grid Under Frequency Protection"
           ##and so on##
      - name: "Fault 2"
        class: ""
        state_class: ""
        uom: ""
        scale: 1
        rule: 1
        icon: 'mdi:wrench'
        isstr: true
        registers: [ 0x0406 ]
        lookup:
          - key: 0
            value: "No error"
          - key: 1
            value: "ID17 Grid current sampling error"
          - key: 2
            value: "ID18 Grid current DC component sampling error (AC side)"
          - key: 4
            value: "ID19 Grid voltage sampling error (DC side)"
         - key: 5
      ##continue like this for other sensors##

I was thinking of something similar to this but I get “TemplateSyntaxError: tag name expected” and I don’t know how to continue for the other sensors:

sensor:
  - platform: template
    sensors:
      stato_faults:
        friendly_name: Fault
        value_template: >
         {% if is_state('sensor.solarman_fault_1', '0') %}
         No Error
         {% else %}
         {% ('sensor.solarman_fault_1') %}  

To output the value your last line should be :

{{ states('sensor.solarman_fault_1') }}

Would that be okay?

sensor:
  - platform: template
    sensors:
      stato_faults:
        friendly_name: Fault
        value_template: >
         {% if is_state('sensor.solarman_fault_1.key', '0') %}
         No Error
         {% else %}
         {{ states('sensor.solarman_fault_1') }}
         {% endif %}

It should return “No error”, instead I get this:

sensor:
  - platform: template
    sensors:
      stato_faults:
        friendly_name: Fault
        value_template: >
         
         unknown

edit: now is ok

Since I have 14 fault sensors with each different key, how can I display just one? Let me explain better with the code and the image.


Screenshot 2023-10-15 at 11-07-17 Panoramica – Home Assistant

What should it display if more than one has an error?

The one or those who have an error. Do you think it is possible?

sensor:
  - platform: template
    sensors:
      stato_faults:
        friendly_name: Fault
        value_template: >
         {% set sensors = states.sensor | selectattr('entity_id', 'search', 'solarman_fault')
         | selectattr('state', 'ne', '0') | map(attribute='name') | list %}
         {% if sensors | count == 0 %}
           No Errors
         {% else %}
           Errors found: {{ sensors | join(', ') }}
         {% endif %}

return

If I replace “search” with “match” I get this. What do you think about it?
Screenshot 2023-10-16 at 05-57-26 Strumenti per sviluppatori – Home Assistant

match only checks against the beginning of the string, so the “sensor.” in each of your entity IDs will cause them to be rejected from the list. You would need to replace entity_id with object_id in the selectattr() if you want to use match.

{%- set sensors = states.sensor | selectattr('object_id', 'match', 'solarman_fault') 
| rejectattr('state', 'eq', '0') | map(attribute='name') | list %}
{%- if sensors | count == 0 %}
  No Errors
{%- else %}
  Errors found: {{ sensors | join(', ') }}
{%- endif %}

Ok but In both cases, right now I should get “No error” since there are no problems, but instead I get:
Errors found: Solarman Fault 1, Solarman Fault 2, Solarman Fault 3, Solarman Fault 4, Solarman Fault 5, Solarman Fault 6, Solarman Fault 7, Solarman Fault 8, Solarman Fault 9, Solarman Fault 10, Solarman Fault 11, Solarman Fault 12

I would need to get “no error” or the sensor value if I have the error.

There something wrong with the state of those sensors… rejectattr('state', 'eq', '0') should remove any of them with a state of ‘0’ so that it doesn’t show up in the list.

Paste the following in your Template editor then share the results:

{{ states.sensor | selectattr('object_id', 'match', 'solarman_fault') 
| map(attribute='state') | list }}
[
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error",
  "No error"
]

So that shows you have sensors that match the search criteria which are returning “No Error” as their state, not “0”… You can find all the sensors that match the criteria with:

{{ states.sensor | selectattr('object_id', 'match', 'solarman_fault') 
| map(attribute='entity_id') | list }}

Did you, maybe, already set up individual template sensors for each solarman sensor?

Whatever the cause, we’ll need to either find a way to filter out sensors we don’t want or use an explicit list of sensors we do want.

Sorry, but I don’t understand what you mean

However, with this code I get:

[
  "sensor.solarman_fault_1",
  "sensor.solarman_fault_2",
  "sensor.solarman_fault_3",
  "sensor.solarman_fault_4",
  "sensor.solarman_fault_5",
  "sensor.solarman_fault_6",
  "sensor.solarman_fault_7",
  "sensor.solarman_fault_8",
  "sensor.solarman_fault_9",
  "sensor.solarman_fault_10",
  "sensor.solarman_fault_11",
  "sensor.solarman_fault_12"
]

According to the two template we have tested you have 12 sensors that are currently returning “No Error” not “0” as you indicated previously. Is “0” a possible value for them to return? When they return something other than “No Error” (or “0”), what should the template sensor return? The entity ID, state, entity name… some other attribute… some combination of those?

Each of the 12 sensors can return 0 bits, 1 bit, 2 bits, 4, 8 and so on. Each bit corresponds to a value. Let’s take the Fault 1 sensor as an example:

      - name: "Fault 1"
        class: ""
        state_class: ""
        uom: ""
        scale: 1
        rule: 1
        registers: [ 0x0405 ]
        isstr: true
        icon: 'mdi:wrench'
        lookup:
          - key: 0
            value: "No error"
          - key: 1
            value: "ID01 Grid Over Voltage Protection"
          - key: 2
            value: "ID02 Grid Under Voltage Protection"
          - key: 4
            value: "ID03 Grid Over Frequency Protection"
          - key: 8
            value: "ID04 Grid Under Frequency Protection"
          - key: 16
            value: "ID05 Leakage current fault"
          - key: 32
            value: "ID06 High penetration error"
          - key: 64
            value: "ID07 Low penetration error"
          - key: 128
            value: "ID08 Islanding error"
          - key: 256
            value: "ID09 Grid voltage transient value overvoltage 1"
          - key: 512
            value: "ID10 Grid voltage transient value overvoltage 2"
          - key: 1024
            value: "ID11 Grid line voltage error"
          - key: 2048
            value: "ID12 Inverter voltage error"
          - key: 4096
            value: "ID13 Anti-backflow overload"
          - key: 8192
            value: "ID14"
          - key: 16384
            value: "ID15"
          - key: 32768
            value: "ID16"

So, if the bit is 0 “no error”, if the bit is 1 "“ID01 Grid Over Voltage Protection” and so on

But, as demonstrated by the previous tests, the state is not returning bits, (at least in the case of “0”/“No error”) it’s returning the descriptive value.

So it’s impossible to get what I want? That is, the descriptive value

No, it’s almost definitely possible… but no one can help you design a working template if the information you provide doesn’t make sense. In you first screenshots

In your previous post for “Fault 1” what is that example? It doesn’t look like the attributes of an entity from the States tool… is it a configuration of some kind? If so what kind? What integration is being used?

Copy the following into your Template editor and share the results:

{{ states.sensor.solarman_fault_1 }}