Unable to create sensor template

I have this in my config.yaml

sensor: !include sensors.yaml

And this in my sensors.yaml file

- platform: template
      sensors:
        alarm_arm_status:
          value_template: >-
            {% if states.binary_sensor.alarm_arm_status.state == 'off' %}
              Disarmed
            {% elif states.binary_sensor.alarm_arm_status.state == 'on' %}
              Armed
          icon_template: >
            {% if states.binary_sensor.alarm_arm_status.state == 'off' %}
              mdi:lock-open
            {% elif states.binary_sensor.alarm_arm_status.state == 'on' %}
              mdi:lock

I keep getting error:

Configuration invalid
Error loading /config/configuration.yaml: mapping values are not allowed here
  in "/config/sensors.yaml", line 2, column 14

What is the correct way to update that sensor, Iā€™m sure my syntax is wrong

{% endif %} required in two places. You should specify an entity id to monitor for the template to update. Also you should capture the unknown state and use this form of template to avoid errors at start up:

- platform: template
    sensors:
      alarm_arm_status:
        entity_id: binary_sensor.alarm_arm_status
        value_template: >-
          {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
            Disarmed
          {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
            Armed
          {% else %}
            Unknown
          {% endif %}
        icon_template: >
          {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
            mdi:lock-open
          {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
            mdi:lock
          {% else %}
            mdi:lock-alert
          {% endif %}

Also, check the indentation for everything after the first line. It all needs moved back to the left 4 spaces in your first post and 2 spaces in toms post.

1 Like

Thanks, I believe I have it right but again the same error
image

Seems to be because of this guy:
image
Is the indentation still wrong? Looks good in VS code

sensors: should be at the same indentation level as platform: Sorry. I copied your error.

Should be:

- platform: template
  sensors:
    alarm_arm_status:
      entity_id: binary_sensor.alarm_arm_status
      value_template: >-
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          Disarmed
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          Armed
        {% else %}
          Unknown
        {% endif %}
      icon_template: >
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          mdi:lock-open
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          mdi:lock
        {% else %}
          mdi:lock-alert
        {% endif %}

ā€œmapping values not allowed hereā€ is nearly always an indentation problem.

Yup that was it, thanks
image

1 Like

But it looks like the icon part worked ok but the value did not, still showing Open/Closed
image
I assumed the Open above should be changed with Armed?

The icon is not correct either. Thatā€™s a shield not a lock.

Are you sure you have the correct sensor?

It should be: sensor.alarm_arm_status

I forgot I have this in my customize.yaml

binary_sensor.alarm_arm_status:
        icon: mdi:shield-check

But yeah I remove that and the icon does not work either

image

And yeah the name looks good

image

No, it does not.

sensor.alarm_arm_status

not

binary_sensor.alarm_arm_status

Still no joy :frowning:

- platform: template
  sensors:
    alarm_arm_status:
      entity_id: sensor.alarm_arm_status
      value_template: >-
        {% if is_state('sensor.alarm_arm_status', 'off') %}
          Disarmed
        {% elif is_state('sensor.alarm_arm_status', 'on') %}
          Armed
        {% else %}
          Unknown
        {% endif %}
      icon_template: >-
        {% if is_state('sensor.alarm_arm_status', 'off') %}
          mdi:lock-open
        {% elif is_state('sensor.alarm_arm_status', 'on') %}
          mdi:lock
        {% else %}
          mdi:lock-alert
        {% endif %}

Sure it is not with the binary in front like in the card?

No, no, no.

Donā€™t change the sensor template. Put that back to using the binary sensor.

Then put this in an entities card in the front end:

sensor.alarm_arm_status

not

binary_sensor.alarm_arm_status

You made a template sensor not a template binary sensor.

Sorry, getting confused thought the template was just being applied to the binary sensor and not creating a new one, however I cannot find the new one in the entity list

- platform: template
  sensors:
    alarm_arm_status:
      entity_id: sensor.alarm_arm_status
      value_template: >-
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          Disarmed
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          Armed
        {% else %}
          Unknown
        {% endif %}
      icon_template: >-
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          mdi:lock-open
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          mdi:lock
        {% else %}
          mdi:lock-alert
        {% endif %}

I appreciate the help, I think Iā€™m making this harder than it is

You missed one

- platform: template
  sensors:
    alarm_arm_status:
      entity_id: binary_sensor.alarm_arm_status  #### <-----HERE
      value_template: >-
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          Disarmed
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          Armed
        {% else %}
          Unknown
        {% endif %}
      icon_template: >-
        {% if is_state('binary_sensor.alarm_arm_status', 'off') %}
          mdi:lock-open
        {% elif is_state('binary_sensor.alarm_arm_status', 'on') %}
          mdi:lock
        {% else %}
          mdi:lock-alert
        {% endif %}

All of this should be in your sensors.yaml file. Not binary_sensors.yaml

Tried that too and same but I reckon my config.yaml is the issue
At the top:
image

And I found this later on
image

I am guessing it is totally ignoring my sensors file because of the other sensor mention? Going to try and put all of them in the file and see if it fixes it

Thatā€™s bad.

Move all the config below that sensors: (minus sensors:, delete that) to your sensors.yaml file.

you should only have one sensors: in your configuration.yaml file and thatā€™s the one pointing to sensors.yaml.

Yeah I was expecting the usual error cannot have multiple sensors etc
Tried the most basic example from https://www.home-assistant.io/components/template/ and just cannot get past the indentation error I think

my sensors.yaml file currently

  - platform: template
      sensors:
        - solar_angle:
            friendly_name: "Sun angle"
            unit_of_measurement: 'degrees'
            value_template: "{{ state_attr('sun.sun', 'elevation') }}"

config.yaml
image

And same error
image

I know that this works fine directly in config.yaml since I can see the new sun sensor in the entity list

sensor:
  - platform: template
    sensors:
      solar_angle:
        friendly_name: "Sun angle"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

The two sensor configs you posted are different.

This is wrong:

  - platform: template
      sensors: ##### <-----  you indented this again.
        - solar_angle: ################ <-- remove the dash, fix the indentation
            friendly_name: "Sun angle"
            unit_of_measurement: 'degrees'
            value_template: "{{ state_attr('sun.sun', 'elevation') }}"

This is right:

- platform: template
  sensors:
    solar_angle:
      friendly_name: "Sun angle"
      unit_of_measurement: 'degrees'
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"

Thereā€™s two types of multiple item objects. Lists, that use ā€œ-ā€ and maps that dont.
Template sensors are maps, not lists. See the ā€œ(map)ā€ in the documentation, https://www.home-assistant.io/components/template/#sensors

Worked it out thanks, it was a combination of - and the sensors: under - platform that was not indented properly. Never thought Iā€™d say it but I think I prefer JSON :smiley:

One more question, the status of the new sensor.alarm_arm_status doesnā€™t seem to be instantly updated when the state of binary_sensor.alarm_arm_status changes. Any idea why?