Defining an "on" state of a switch using value_json

Hello everyone,

I always had my pellet heater on a sonoff sv with a zero voltage relay. I wired the relay in parallel (with a pulsetime in console) to the pellet heater momentary switch - that I need to hold for 3 seconds, to power it on or off.
The downside of this method is that in HA the on/off state would represent the state of the relay, which does not match the actual power state of the pellet heater. But it was fine for my use case, I just removed the state topic and used payload off = on , so it would also toggle the relay if turned off.

However I just wired in a Pow R2 to the heater, that I want to use to define the state of the heater.
I have set up the sensor in mqtt for the power and total consumption and it shows up fine in HA:

  - platform: mqtt
    name: "Kachel vermogen"
    state_topic: "tele/powr2/SENSOR"
    value_template: “{{ value_json['ENERGY'].Power }}”
    unit_of_measurement: "W"

I now want to define an “on” state for the pellet heater if the power is above 20 watt and anything below that is off and have come up with some searching with this -probably not functional- uncommented code.

switch:
  - platform: mqtt
    name: "Kachel"
    command_topic: "cmnd/kachel1/POWER"
#   state_topic: "tele/powr2/SENSOR"
#   value_template: "{{ value_json['ENERGY'].Power 'on' if ( state.state  > 20 ) else 'off' }}"
    qos: 1
    payload_on: "ON"
    payload_off: "ON"

Do you guys think this code will work and it will be the best way of going about doing this?

You might look at the template binary sensor

Thanks. At this point I’m trying to understand how to go about defining an on state for a switch by adding a binary sensor with template. You sure adding a binary sensor will allow me to define the on state of my Kachel switch? You reckon you got some pointers for me? So far I’ve always been reliant on examples to set up my home automation to my needs and I’ve gone a long way, but this stuff is mind boggling.