Problems with a template sensor

hello everyone,

I have this code and have been looking for a few hours to see what is wrong here, but I can’t find it.

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: Nachtmodus-Icon2
        state: "{{ is_state('input_boolean.nachtmodus', 'on') }}"
        picture: >
          {% if is_state('input_boolean.nachtmodus', 'on') %}
            /local/custom_icons/zon1.svg
          {% else %}
            /local/custom_icons/maan1.svg
          {% endif %}


the files are in this folder
config\www\custom_icons.
can someone help me further and tell me what is wrong? thanks in advance.

Looks like a naming mismatch?
change input_boolean.nachtmodus to input_boolean.nachtmodus2

thanks for your response, I have now changed it to

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: Nachtmodus-Icon2
        state: "{{ is_state('input_boolean.nachtmodus2', 'on') }}"
        picture: >
          {% if is_state('input_boolean.nachtmodus2', 'on') %}
            /local/custom_icons/zon1.svg
          {% else %}
            /local/custom_icons/maan1.svg
          {% endif %}


but it makes no difference

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: nachtmodes_icon2
        state: {{ is_state('input_boolean.nachtmodus2', 'on') }}
        attributes:
          entity_picture: >
            {% if is_state('input_boolean.nachtmodus2', 'on') %}
              /local/custom_icons/zon1.svg
            {% else %}
              /local/custom_icons/maan1.svg
            {% endif %}

Try this above.

That state template needs to be surrounded by quotes and the picture configuration variable doesn’t go under attributes.

Are you reloading the Template entities or restarting HA after making the changes?

What do you mean by “it makes no difference”… how are you testing it?


The entity picture is working as expected for me when I use the configuration that you posted with some random .svg files:

image

image

The exact config used
input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - sensor:
      - name: "Nachtmodus Icon2"
        picture: |
          {% if is_state('input_boolean.nachtmodus2', 'on') %}
          /local/custom_icons/ball_of_yarn.svg
          {%else%}
          /local/custom_icons/bandit_mask_dj.svg
          {%endif%}
        unique_id: nachtmodus_0002
        state: "{{is_state('input_boolean.nachtmodus2', 'on')}}"
1 Like

Thanks for your reply. I keep restarting it.

What do you mean by “it makes no difference”… how are you testing it?

Yes, I adjusted what my supervisor asked me to do, but it doesn’t change anything.

I think I’ve tried everything already, but I can’t get it to work.

Did you try what @Didgeridrew suggested?

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: nachtmodes_icon2
        state: "{{ is_state('input_boolean.nachtmodus2', 'on') }}"
        picture: >
          {% if is_state('input_boolean.nachtmodus2', 'on') %}
            /local/custom_icons/zon1.svg
          {% else %}
            /local/custom_icons/maan1.svg
          {% endif %}

Thank you for your reply. This doesn’t work at all; I immediately get this error:

What I find so strange is that this code:

input_boolean:
  nachtmodus:
    name: Nachtmodus

template:
  - binary_sensor:
      - name: "Nachtmodus Icon"
        unique_id: Nachtmodus-Icon
        state: "{{ is_state('input_boolean.nachtmodus', 'on') }}"
        icon: >
          {% if is_state('input_boolean.nachtmodus', 'on') %}
            local:maan1
          {% else %}
            local:zon1
          {% endif %}

with the folder path: config\custom_icons does work

but this one with the folder path: config\www\custom_icons

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: Nachtmodus-Icon2
        state: "{{ is_state('input_boolean.nachtmodus', 'on') }}"
        picture: >
          {% if is_state('input_boolean.nachtmodus', 'on') %}
            /local/custom_icons/zon1.svg
          {% else %}
            /local/custom_icons/maan1.svg
          {% endif %}

does not work.

Does anyone perhaps have another idea?

Share the whole file. You’ve shown us 15 lines and the error is calling out line 24. I.e. You’re hiding the problem from us.

Where does this YAML code exist?

The error message indicates you are using packages.

Thank you for your response.

This is the complete code and is located at:

config\Packages\Helpers\input_boolean\nachtmodus

… post the entire code for

input_boolean:
  nachtmodus2:
    name: Nachtmodus2

template:
  - binary_sensor:
      - name: "Nachtmodus Icon2"
        unique_id: Nachtmodus-Icon2
        state: "{{ is_state('input_boolean.nachtmodus', 'on') }}"
        picture: >
          {% if is_state('input_boolean.nachtmodus', 'on') %}
            /local/custom_icons/zon1.svg
          {% else %}
            /local/custom_icons/maan1.svg
          {% endif %}

This is the entire code and it is in this folder: config\Packages\Helpers\input_boolean\nachtmodus
and the filename is: nachtmodus2.yaml

That’s not possible. The error is calling out line 24. That file only contains 15 lines. You are looking at the wrong file, or that’s not the error you are getting.

1 Like

Possible shadowed automation? Another copy in the registry I just had to excise one out of mine. It was a royal pain…

This is a config file, not automation

1 Like

Sorry template but it is still entirely possible to setup a template binary sensor in the UI with the same name and ent ID and then accidentally shadow it with yaml…

Ask me how I know.

No, that’s not possible. You’re mixing up 2 separate systems.

The error explicitly points to a yaml file, it explicitly points to a line number. The only way this will occur is if the file is being read and it has that many lines. Regardless if a mirrored entity shares the same entity_id in the ui.

1 Like

Is that the only package file or are there others?

Please post the section of your configuration.yaml file that references all of your package files.