Need help for mapper template

Hi,
I’ve tried the mapping template for the first time to map a numeric sensor to a string.
But it doesn’t work :frowning:

There are many threads in this forum using “value_template” for mapping.
But when using it instead of “state”, there is an error

property value_template is not allowed

and

missing property “state”

template:
 - sensor
   - name: Wärmepumpe - Betriebszustand
     unique_id: gw.warmepumpe_status_sg_ready_betriebszustand
     value_template: >-
       {% set mapper =  {
           1 : 'Zustand 1',
           2 : 'Zustand 2',
           3 : 'Zustand 3',
           4 : 'Zustand 4' } %}
       {% set x = states.sensor.warmepumpe_status_sg_ready_betriebszustand_numeric.state %}
       {{ mapper[x] if x in mapper else 'Failed' }}

The sensor doesn’t appear in HASS. When using “state” it works, but without mapping.

Can anybody help me?

…change value_template: in state:
EDIT, sorry… did not read the last line

template:
  - sensor:
      - name: Wärmepumpe - Betriebszustand
        unique_id: gw_warmepumpe_status_sg_ready_betriebszustand
        state: "Zustand {{ states('sensor.warmepumpe_status_sg_ready_betriebszustand_numeric') }}"

The template can be enhanced if you let me know the constraints. For example, if the values can only be less than 4, we can do this:

template:
  - sensor:
      - name: Wärmepumpe - Betriebszustand
        unique_id: gw_warmepumpe_status_sg_ready_betriebszustand
        state: >
          {% set x = states('sensor.warmepumpe_status_sg_ready_betriebszustand_numeric') | int(0) %}
          {{ 'Zustand ' ~ x if x <= 4 else 'Failed' }}

NOTE

Correction. It’s state not value_template. This is a Template Sensor in modern format, not legacy format.


EDIT

I’m not sure if periods are allowed in the value of unique_id (it should be a slug which doesn’t allow using any character) so I changed it to an underscore.

as the sensor kicks back strings, I guess this may work better

{% set mapper =  {
           '1' : 'Zustand 1',
           '2' : 'Zustand 2',
           '3' : 'Zustand 3',
           '4' : 'Zustand 4' } %}
       {% set x = states.sensor.warmepumpe_status_sg_ready_betriebszustand_numeric.state %}
       {{ mapper[x] if x in mapper else 'Failed' }}

The sensor doesn’t appear because it has a syntax error.

You have defined a Template Sensor in modern format yet you used the name of an option that is only supported by legacy format.

In modern format, the option’s name is state not value_template.


NOTE

Unless this application has a more complex requirement, the use of a map is overkill for appending a number to the string ‘Zustand’. A simple template can do that as shown in my example above.

The OP mentioned having used ‘state’ but from what I see mapping numeric with string won;t work (or?)
Then…you solution is of course neater :slight_smile:

The example in the first post contains two mistakes:

  1. It used value_template instead of state in a modern format Template Sensor which caused the error “property value_template is not allowed”.

  2. Changing value_template to state fixed one problem but exposed another, namely the template contains an error. The map keys are numeric but the sensor’s value is a string. The quick fix is to use int(0) to convert the sensor’s value from string to integer.

However, as mentioned, there’s no need for a map to perform a simple string concatenation. The example I posted above is syntactically correct and uses simple templating to combine Zustand and the sensor’s value.

Thank you for your support!

I’m still extremely insecure configuring sensors in modern and legacy format… This is extremely confusing while there a many tutorials and snippets in the other format. But I’m still learning.

You’re right, it doesn’t make sense just to merge a string with the number :wink: I’m very lazy, therefore I’ve just wrote down an example to test my script :wink: Later there will be individual, meaningful strings to show a status.

And to convert to an int(0) is also a good idea!

Thanks!!!

If your real plan is to use “meaningful strings” instead of merely the sensor’s numeric value then you can use a map like this:

template:
  - sensor:
      - name: Wärmepumpe - Betriebszustand
        unique_id: gw_warmepumpe_status_sg_ready_betriebszustand
        state: >
          {% set x = states('sensor.warmepumpe_status_sg_ready_betriebszustand_numeric') | int(0) %}
          {{ {1: 'Your first string',
              2: 'Your second string',
              3: 'Your third string',
              4: 'Your fourth string'}.get(x, 'Failed') }}