Change the color of the icon of a virtual sensor depending of the state

Here’s one of the examples from the documentation:

    sensor.synology_status_volume_1:
      templates:
        icon_color: >
          if (state == 'warning') return 'red';
          return 'green';

None of the examples you posted includes the template: option.

Try this in the customize.yaml file:

  sensor.sensor_digital_input_1_on_off:
    templates:
      icon_color: >
        if (state == 'OFF') return 'red';
        return 'green';