Mushroom Template Card - Change Icon Color Based on Power Draw Value

Hey there! This post isn’t particularly special in my opinion, I’m simply just trying to get a small piece of code to work , shown below:

{{ (states(“switch.tp_link_power_strip_f567_pc_plug”)|float(8) < 9 ) | iif(“green”, “red”) }}

Essentially I’m trying to detect whether my PC is running or not based on it’s power draw using an icon color indicator in mushroom’s template card. It seems to work somewhat so far, as the color does change if I adjust the values manually, though it won’t change automatically based on power draw. It seems I’ve simply made a coding error somewhere, as I’m not familiar with YAML, so I couldn’t say what the error could be. If anyone could let me know how to get this working I’d greatly appreciate it.

Edit: For context, I’m attempting to turn the icon red when power draw is equal to or below eight watts, and turn it green when it is equal to or above nine watts.

Does your “Switch-plug” ( State ) in fact shows an int ?, a switch is “normally” a binary ( on/of )

As for your template , try to remove your “double quotes”, and use ‘single quote’

PS: In /Developer-Tools/#template , you can test your templates

Just to clarify, I only got into this stuff a couple weeks ago, and while I’m pretty tech savvy, I’m not very coding savvy, so I might not understand any possible code or HA specific terms right out of the gate. As for your question, admittedly not sure what you mean by “int” but I can confirm the plug has native energy monitoring functionality that works in HA when viewed from the devices section.

int is a number
In / Developer-Tools you can also see your entitites “State” and attributes

A Sensor deliver the power/energy , a switch can provide this, but not in the “switch State” , but maybe as an attributes ( i think this is now “removed” and individual sensors is made for i.e Power (W) as well as for Energy (kWh)

You’ll probably understand this more than I do, but given it has a measurement attribute I assume what I’m trying to do is possible. Again, it’s likely just a small coding error, I’d guess with the float value or the number next to it, though as I said I’m not really sure. Here are the following attributes for the plug itself.

state_class: measurement
unit_of_measurement: W
device_class: power
friendly_name: TP-LINK_Power Strip_F567 PC Plug current consumption

and what’s the “entity name” ? … switch or sensor ?

Just checked to be sure, but it’s a sensor.

1 Like

so, replace switch with sensor … it should work, with single quotes

PS: however this " |float(8) < 9 ) " i don’t know, never used this “float(8)” , or the whole template for that matter

Got it, it recognized it as a valid entity, so at least that portion is correct now. Now, if I wanted to detect it’s power draw under the conditions I specified (aka 8 watts and under equals “off” and 9 watts and above equals “on”, what values would I need to adjust, if any? Apologies for the lack of understanding, I’m sure I’ll get better with time.

That’s okay, you still helped out a bit and that’s what counts. Hopefully someone can understand this code better than me or you since it clearly works in some way, I’m just not sure how it works. I suppose we’ll see.

Get use to read the Doc’s, start with what i linked for you

{{ (states(‘sensor.yours’) <= ‘x’) | iif(‘on’, ‘off’) }}

Figured it out! Just had to adjust the code a little bit and it worked as intended. Leaving this here if anyone wants to copy paste this into the icon color section of a mushroom template card.

{{ (states(“sensor.your_sensor”)|float(0) > Insert the target wattage here ) | iif(“green”, “red”) }}