Swapping fan icon

I have a fan that will send an MQTT topic to show whether it is running or not.

I do not want to control the fan, just have it report its state, which does quite happily.

but I cannot find a device class for a fan binary sensor.

is there a way to swap between the two fan icons to show whether it is running or not?
mdi:fan
mdi: fan-off

Many thanks

You can use icon_template

Thank you arsaboo, I did look at that, but i was lost…

I have split the configuration into subdirectories, so have a binary sensors folder, where would i need to record templates.

and how do i use templates to change the icon?

thank you again

You can use it in sensors or in customize (see here for an example).

Sorry i am being really thick here.

I have tried:
binary_sensor.rad_fan:
templates:
icon: >
if (state === ‘on’) return ‘mdi:fan’;
return ‘mdi:fan-off’;
in a file in the binary_sensors directory

I have tried adding it to the end of the sensor its self:

  • platform: mqtt
    name: “Rad Fan”
    state_topic: “bedroom/Radfan”
    payload_on: “On”
    payload_off: “OFF”
    icon_template:
    icon: >
    if (state === ‘on’) return ‘mdi:fan’;
    return ‘mdi:fan-off’;

neither of which will shoe the icon.

really sorry

On is not the same as on, case matters.

sorry should have said, tried both

Also I think === is wrong.

i took that from the files that arsaboo shared

Scratch that response I was reading the wrong page

ah ok, not helping iat the moment that i lost all my previous work when the pi died!!!