ecchodun
(Ecchodun)
1
Without getting into templates, how can I check the value of a specific entity in custom YAML for a dashboard. Specifically, something like this -
if (state('media_player.stereo_denon_zone2') == 'playing' )
return ``
The code above clearly doesn’t work.
Rofo
(Ro)
2
I think you mean
if (states('media_player.stereo_denon_zone2') == 'playing' )
return ``
Note the missing S.
Or better still
if (is_state('media_player.stereo_denon_zone2') , 'playing' ))
return ``
ecchodun
(Ecchodun)
5
So I got the code to work with the following:
custom_fields:
volume: |
[[[
if (states['media_player.stereo_denon_zone2'].state == 'off')
return ``
if (states['media_player.stereo_denon_main_zone'].state != 'off')
return `<ha-icon
icon="mdi:volume-high"
style="width: 18px; height: 17px; color: deepskyblue;">
</ha-icon><span> Volume: <span style="color: var(--text-color-sensor);">${Math.round((states['media_player.stereo_denon_zone2'].attributes.volume_level)*100)}</span></span>`
if (states['media_player.stereo_denon_zone2'].state != 'off')
return `<ha-icon
icon="mdi:volume-high"
style="width: 18px; height: 17px; color: deepskyblue;">
</ha-icon><span> Volume: <span style="color: var(--text-color-sensor);">${Math.round((states['media_player.stereo_denon_zone2'].attributes.volume_level)*100)}</span></span>`
if (states['media_player.stereo_denon_zone3'].state != 'off')
return `<ha-icon
icon="mdi:volume-high"
style="width: 18px; height: 17px; color: deepskyblue;">
</ha-icon><span> Volume: <span style="color: var(--text-color-sensor);">${Math.round((states['media_player.stereo_denon_zone2'].attributes.volume_level)*100)}</span></span>`
]]]
Is there any way to consolidate the three if statements into one?