Remapping sensor values not working

Hi community members,

I’m a little bit new to the coding part of HA. I’m trying to remap the values of a group I created. By default it has two states ‘on’ and ‘off’, but I want change this to the Dutch words ‘aan’ and ‘uit’.

sensor:
  - platform: template
    sensors:
    buitenverlichting:
        value_template: >-
            {% if state.group.buiten.state == 'off' %}
            "Uit"
            {% elif state.group.buiten.state == 'on' %}
            "Aan"
            {% else %}
            "n/a"
            {% endif %}

I’ve pasted the code above in my configuration.yaml and the ‘Check configuration’ says it’s all good. Although I can’t seem to find this sensor within Developer Tools > States where it should pop-up I assume.

Can someone point me in the right direction? Thanks!

Cheers,
Per

Your indentation is not correct. The slug (buitenverlichting:) must be indented two spaces under sensors:. Also don’t quote the items in your template results.

Finally there’s this warning you should take note of:

https://www.home-assistant.io/docs/configuration/templating/#states

Putting all that together gives:

sensor:
  - platform: template
    sensors:
      buitenverlichting:
        value_template: >-
          {% if is_state('group.buiten', 'off') %}
            Uit
          {% elif is_state('group.buiten', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}
1 Like

Thanks for the clarrification on how to use spaces, quotes etc. This helps a lot.
Unfortunately the code still doens’t work. When I try to use ‘buitenverlichting’ it still doens’t show up in “Developer Tools > States” and also shows no value when I try to use it within my banner-card.

Is this me?

Edit #1:
Fixed by changing it to:

template:
    sensors:
      buitenverlichting:
        value_template: >-
          {% if is_state('group.buiten', 'off') %}
            Uit
          {% elif is_state('group.buiten', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}

Why does this work and your code didn’t? Can anyone explain so I do understand what I did here? :slight_smile:

Edit #2::

I really starting to think I’m missing some kind of basic principal here since I try to do the same for a different sensor and…it doesn’t work the same. I’ll share some more of the code around it because I’ve got the feeling it’s all connected:

template:
  - sensor:
      - name: "badkamer_temp"
        state: "{{state_attr('climate.badkamer', 'current_temperature')}}"
  - sensor:
      - name: "kantoor_temp"
        state: "{{state_attr('climate.begane_grond', 'current_temperature')}}"

template:
    sensors:
      buitenverlichting:
        value_template: >-
          {% if is_state('group.buiten', 'off') %}
            Uit
          {% elif is_state('group.buiten', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}
          
template:
    sensors:
      tvkantoor:
        value_template: >-
          {% if is_state('remote.sony_bravia_tv', 'off'} %}
            Uit
          {% elif is_state('remote.sony_bravia_tv', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}

The first part of the code above worked for weeks. The second part for the ‘buitenverlichting’ also did work with the latest change. But after adding that same code for remote.sony_bravia_tv, all off the code got corrupted it seems.

I’m really doing something wrong here and kinda feel stupid now but I can’t see what it is.

The template integration does not use a format like that.

I think I’m getting the hang of it. I’ve got all sensors working now and I really do hope I’m using the right way/best practice here:

template:
  - sensor:
    - name: badkamer_temp
      state: "{{state_attr('climate.badkamer', 'current_temperature')}}"
  
  - sensor:
    - name: kantoor_temp
      state: "{{state_attr('climate.begane_grond', 'current_temperature')}}"

  - sensor:
    - name: buitenverlichting
      state: "{% if is_state('group.buiten', 'off') %}
                Uit
              {% elif is_state('group.buiten', 'on') %}
                Aan
              {% else %}
                n/a
              {% endif %}"
  - sensor:        
    - name: tvkantoor
      state: "{% if is_state('remote.sony_bravia_tv', 'off') %}
                Uit
              {% elif is_state('remote.sony_bravia_tv', 'on') %}
                Aan
              {% else %}
                n/a
              {% endif %}"

Feedback is more than welcome!

1 Like

Better. You’ve used a combination of multi-line and single line template that looks a bit odd and your indentation is not consistent. ideally you would do this:

template:
  - sensor:
      - name: badkamer_temp
        state: "{{state_attr('climate.badkamer', 'current_temperature')}}"
  
  - sensor:
      - name: kantoor_temp
        state: "{{state_attr('climate.begane_grond', 'current_temperature')}}"

  - sensor:
      - name: buitenverlichting
        state: >
          {% if is_state('group.buiten', 'off') %}
            Uit
          {% elif is_state('group.buiten', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}
  - sensor:        
      - name: tvkantoor
        state: >
          {% if is_state('remote.sony_bravia_tv', 'off') %}
            Uit
          {% elif is_state('remote.sony_bravia_tv', 'on') %}
            Aan
          {% else %}
            n/a
          {% endif %}
1 Like

Check, I’m getting there.
All sensors are working now so…that’s great! :slight_smile:

Thanks a lot for your help so far, I’m going to reformat the rest of my configuration.yaml so it all is consistent and readable.

Using VSCode with the Rainbow indent extension helps. It highlights bad indentation:

1 Like

That’s a great tip, I already use the VSCode but I’ll add the Rainbox indent extension as well.