Value_template for state_topic

Hello everyone,

So far 3 months in I’ve been able to set up my HA by myself but now I’m running into some issues that I’m unable to figure out. No amount of reading can help me as I’m too much of a noob I guess.

So I get this sensor information from my recently installed powr2.

07:49:07 MQT: tele/powr2/SENSOR = {"Time":"2019-05-12T07:49:07","ENERGY":{"TotalStartTime":"2019-05-04T10:35:46","Total":4.556,"Yesterday":0.260,"Today":0.070,"Power":18,"ApparentPower":130,"ReactivePower":129,"Factor":0.14,"Voltage":238,"Current":0.546}}

And I wanna define an on state for my switch:

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

And it’s not really working out. I’ve tried to look up in multiple topics how to go about doing this and this code is just one of many I’ve tried. I try my value template under DEV > templates and it always comes back as undefined no matter what I do.

I really could use some assistance. :frowning:

Where have you seen this code?

value_template: "{{ value_json.ENERGY.Power > 10 }}"

should do it.

1 Like

Thanks. That’s a good question. I’ve scoured through so many topics and tried so much code, I wouldn’t really know where I got it.

However if I add this line the state of the switch is still defined by the state of the relay. Under dev > templates I get “UndefinedError: ‘value_json’ is undefined”, , which is weird since I’ve also set up a sensor and it shows the value correctly in HA frontend. The value template of that sensor also shows this error message.

It’s a tasmotized Sonoff SV with pulsetime of 3, I’ve wired this in parallel to the momentary switch of my pellet heater, and the pulsetime is to simulate a 3 second button press for turning the heater on and off.