Help needed with a multi state template entity and icons

Hello,

I installed a power monitoring Shelly 1+ PM this week to alert me when my washing machine/tumble dryer are finished.

I wanted to set up a custom entity to quickly estimate what the current state of the appliances are (both are connected to the same FCU) and ended up with the following.
Please note, I’m awful at coding and usually steal someone else’s work and butcher it for my own purposes.

  - sensor:
      - name: Laundry Status
#      unique_id: laundry_status
#        friendly_name: "Laundry Status"
#        device_class: power
        state: >
          {% if states("sensor.shelly_laundry_switch_0_power") | float(0) == 0 %}
            off
            icon: mdi-washing-machine-off
          {% elif states("sensor.shelly_laundry_switch_0_power") | float(0) <= 3 %}
            standby
            icon: mdi-washing-machine-off
          {% elif states("sensor.shelly_laundry_switch_0_power") | float(0) >= 250 %}
            drying
            icon: mdi-tumble-dryer
          {% else %}
            washing
            icon: mdi-washing-machine
          {% endif %}

All is working well except the icons for the various states. The way I have it at the moment, the icon strings are just appearing as a part of the state displayed when I add the entity to one of my dashboards (ie the dryer is currently on and instead of the state displaying as “drying” it’s displaying " drying icon: mdi-tumble-dryer ")

It’s clearly a skill issue on my part but can anyone point me in the right direction?

The main issue is that you are trying to build YAML configuration with templates, which will not work. Templates are only able to provide values, not the entire key: value pair. Also you have a couple typos

  - sensor:
      - name: Laundry Status
#      unique_id: laundry_status
#        friendly_name: "Laundry Status"
#        device_class: power
        state: >
          {% set power = states("sensor.shelly_laundry_switch_0_power") | float(0) %}
          {% if power == 0 %}
            off
          {% elif power <= 3 %}
            standby
          {% elif power >= 250 %}
            drying
          {% else %}
            washing
          {% endif %}
        icon: >
          {% if this.state == 'washing' %} mdi:washing-machine
          {% elif this.state == 'drying' %} mdi:tumble-dryer
          {% else %} mdi:washing-machine-off
          {% endif %}

Thank you.

I’m not sure where the typos were and I’m not sure what you mean by the key: value pair point, but it looks like you’ve used % set power = states... to set a variable to streamline the rest of the configuration and then separately set the icon based on the state of the sensor?

So I assume (in laymans’ terms) that what you’d describe as a template is the section between state: > and icon: > and that it is only used to update the state and can’t be used to update anything else at the same time, with other changes requiring a separate section?

mdi-washing-machine-off is not a valid value, it should be mdi:washing-machine-off

https://www.commonwl.org/user_guide/topics/yaml-guide.html#key-value-pairs

state and icon are both keys… they each need to be defined individually. By including icon in the template for state you were essentially saying “This should be considered part of the state’s value”.

For Home Assistant purposes, a “template” is any portion of a configuration written in the Jinja templating language, with some limitations and additional Home Assistant specific functions

Setting the variable isn’t really necessary, but it’s easier to read and very slightly more efficient. It has no effect on the rest of the configuration, just the template for the value of the state. The value for the icon uses the self-referencing variable.

1 Like