Having trouble with value_templates

I am trying to switch on my hot water when teh solar is producing enough and the good lady isn’t cooking. The hot water draws 4kW which I am using as threshold. I have crafted the value_template:

condition: template
value_template: -
“{{ ((states(‘sensor.solar_power’) | float(0)) -
(states(‘sensor.load_power’) | float(0)) > float(4))}}”

I’ve tested it in Developer Tools - Templates and if works returning “True” and “False” but when I test the same code in the automation it comes up “Condition did not pass”. Is there something I’m missing in the way automatons work?

The automation cut and pasted:

alias: Turn Hot Water On
description: Turn on Hot Water if Solar production is > 4kW
triggers:
  - trigger: time_pattern
    minutes: /1
conditions:
  - condition: state
    entity_id: switch.sswitch_0
    state:
      - "off"
  - condition: template
    value_template: >-
      "{{ ((states('sensor.solar_power') | float(0)) -
      (states('sensor.load_power') | float(0)) >
      float(2))}}"
actions:
  - type: turn_on
    device_id: some device
    entity_id: some entity
    domain: switch
mode: single

It’s a bit hard to read your code when it’s not formatted correctly.
But this part: >- means you should not have quotes around the template.
So remove the " " from the template and try again.

Keep in mind that if your load sensor fails then this will almost always be true.
You should include a condition that load sensor has a proper value.

I’v tried with and without " and still the same lack of results.

When the template is like this:

  - condition: template
    value_template: "{{ ((states('sensor.solar_power') | float(0)) -(states('sensor.load_power') | float(0)) > float(2))}}"

then it requires the quotes.
But as you have it, you need to remove them.
Sometimes the editor switches between the two and confuses people.

  - condition: template
    value_template: >-
      {{ ((states('sensor.solar_power') | float(0)) -
      (states('sensor.load_power') | float(0)) >
      float(2))}}

With quotes the parser will read it as a string.

You were correct. The sun has gone down here and the condition was supposed to fail. It now works.

Thanks for the tip about when the quotes a er needed as it is confusing.

Don’t know if you saw the edit I did before about adding another condition to make sure load sensor is valid value.
Your template will make it default to 0.0 meaning solar power will most likely be larger and thus switch on the water heater.