Is it possible to have Glance toggle action fire MQTT publish?

I’m trying to set up a glance card that contains status information on all the smart outlets in my house:

columns: 4
  - entity: sensor.refrigerator_power_consumption
  - entity: sensor.current_meter_usage
  - entity: sensor.washer_power_consumption
  - entity: sensor.sonoff_s31_1_power
      action: toggle
  - entity: sensor.sonoff_s31_2_power
  - entity: sensor.sonoff_s31_3_power
      action: toggle
title: Power Usage
type: glance

This gives me the following

My sensors are defined like this:

- platform: mqtt
    name: "Sonoff S31-1 Energy"
    state_topic: "tele/sonoffS31-1/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Today"] }}'
    unit_of_measurement: "kWh"

For some of these, I want to be able to turn them on/off via the UI. From searching the forums here, I found the “tap_action”, so I added that to the “stereo rack” element, but when I click it I get a “failed to call sensor/turn_off”. From further googling (30-45 minutes last night), I can’t seem to find a way for sensors to do a toggle via mqtt publish… something like this is what I’m trying to do

  - service: mqtt.publish
      entity_id: >
      {% if states.sensor.sonoff_s31_1_power > 0 %}
            topic: ‘cmnd/sonoffS31-1/POWER’
          payload: “off”
          {% else %}
            topic: ‘cmnd/sonoffS31-1/POWER’
          payload: “on”
          {% endif %}. 

this seems to work for switches, as you can toggle switches on/off, but i can’t seem to find a way to toggle mqtt based off of a sensor entity only. Am I missing something?

I’ve sort of solved this myself


- platform: mqtt
    name: "Office PCs MQTT Switch"
    state_topic: "tele/sonoffS31-1/STATE"
    value_template: '{{ value_json["POWER"] }}'
    command_topic: "cmnd/sonoffS31-1/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"

in lovelace config:

- entity: sensor.sonoff_s31_1_power
      action: call-service
      service: switch.toggle
        entity_id: switch.office_pcs_mqtt_switch

The only thing this doesn’t do is change the icon, I think I need to use a template switch for this functionality, but at least clicking the icon toggles the mqtt publish on/off.