neilimixamo / Home-Assistant-Quick-Look-Mobile

127 stars 6 forks source link

Icon change depending on sensor status with AND condition #17

Closed hAMMERlized closed 1 year ago

hAMMERlized commented 1 year ago

Hey man,

i am trying to change my icon depending on the state of 3 sensors (on / off), but i struggle with a AND condition.

This is my code so far:

icon:  |
    [[[
    var discharging = states['input_boolean.dummy_battery_discharging'].state;
    var charging = states['input_boolean.dummy_battery_charging'].state;
    var generating = states['input_boolean.dummy_pv_generating'].state;
    return discharging === 'on' ? 'mdi:battery-arrow-down' : 
    charging === 'on' ? 'mdi:battery-arrow-up' : 
    generating === 'on' ? 'mdi:solar-power-variant' : 'mdi:solar-panel';
    ]]]

What i need is changing the icon if 2 conditions (in this case 2 input_boolean sensors) are set to ON. For example ifinput_boolean.dummy_pv_generatingAND input_boolean.dummy_battery_charging are ON, display icon XY.

Can you help me figuring out how the code will look like? Thanks.

Regards, Sebastian

neilimixamo commented 1 year ago

Hi Sebastian, I would try something like this :

icon:  |
    [[[
    var discharging = states['input_boolean.dummy_battery_discharging'].state;
    var charging = states['input_boolean.dummy_battery_charging'].state;
    var generating = states['input_boolean.dummy_pv_generating'].state;

    return discharging === 'on' ? 'mdi:battery-arrow-down' :
           (charging === 'on' && generating === 'on') ? 'mdi:XY' :
           charging === 'on' ? 'mdi:battery-arrow-up' : 
           generating === 'on' ? 'mdi:solar-power-variant' : 'mdi:solar-panel';
    ]]]
hAMMERlized commented 1 year ago

Okay so the trick is to use two & signs and to put both variables inside the brackets? Funny i din't figure it out myself. So simple.

Thanks anyway. Will see tomorrow if it works when the sun comes out at start charging the battery.

hAMMERlized commented 1 year ago

Just for info, how about a OR condition? Like sensor "a" OR " sensor "b" are on display icon XY?

Is it two of the horizontal lines || instead of &&?

hAMMERlized commented 1 year ago

It's working. Thanks again!