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.
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 %}
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?
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”.
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.