Template Sensor troubleshooting

Hello

Looking for help with my gates sensor. I have PLC (Wago 750) connected to HA by Modbus TCP. I have a few inputs connected which have few states from a single sensor (Modbus_sensor): 252 (gates opened), 253 (gates closed), 254 (gates malfunction), 255 (gates in progress). Those statuses i’m able to read in sensor.gates. If I put code into http://localhost:8123/developer-tools/template, then statuses will be displayed as well correctly (Opened, closed and etc):

          {% if is_state('sensor.gates', '252') %}
            Opened
          {% elif is_state('sensor.gates', '253') %}
            Closed
          {% elif is_state('sensor.gates', '254') %}
            Malfunction
          {% elif is_state('sensor.gates', '255') %}
            Progress
          {% else %}
            failed
          {% endif %}

But if I put final code to configuration.yaml file:

sensor:
  - platform: template
    sensors:
      gates:
        value_template: >-
          {% if is_state('sensor.gates', '252') %}
            Opened
          {% elif is_state('sensor.gates', '253') %}
            Closed
          {% elif is_state('sensor.gates', '254') %}
            Malfunction
          {% elif is_state('sensor.gates', '255') %}
            Progress
          {% else %}
            failed
          {% endif %}

I’l get only “failed”

What I’m doing wrong?

Try removing the quotes around the state. For example, instead of '252' try 252.

Thanks for answer. If I put same code without quotes into yaml then nothing changed - failed (else).
If I put code without quotes into Template section I’l get failed (else) response.

sensor:
  platform: modbus
  scan_interval: 5
  registers:
    - name: gates
      hub: wago
      slave: 1
      register: 12488
      data_type: int
sensor:
  - platform: template
    sensors:
      gates:
        value_template: >-
          {% if is_state('sensor.gates', '252') %}
            Opened
          {% elif is_state('sensor.gates', '253') %}
            Closed
          {% elif is_state('sensor.gates', '254') %}
            Malfunction
          {% elif is_state('sensor.gates', '255') %}
            Progress
          {% else %}
            failed
          {% endif %}

is that is okay? I got notice that I have two “sensor” strings, but it won’t work with just one

You need to combine both sensor platforms under one sensor: or append a string or number to the end of one of the sensor: keys. See here for more info.

Personally, I think the first option makes more sense in most cases.

1 Like

Thank you! That was a case. Fixed and working now with this yaml code.

sensor:
  - platform: modbus
    scan_interval: 5
    registers:
      - name: gates
        hub: wago
        slave: 1
        register: 12488
        data_type: int
  - platform: template
    sensors:
      sgates:
        value_template: >-
          {% if is_state('sensor.gates', '252') %}
            Opened
          {% elif is_state('sensor.gates', '253') %}
            Closed
          {% elif is_state('sensor.gates', '254') %}
            Malfunction
          {% elif is_state('sensor.gates', '255') %}
            Progress
          {% else %}
            failed
          {% endif %}