Can anyone help with this IF statement?

Hopefully somebody can help as this is driving me nuts! The following code doesn’t work for me, I get this error:

Failed to perform the action badnest.boost_hot_water. invalid boolean value [[[ if (states['water_heater.1st_floor_thermostat_hot_water'].state === "on") return "false" else return "true" ]]] for dictionary value @ data['boost_mode']. Got None

Any ideas? My code below which I’m testing in Actions. If I edit the code to just set boost_mode: true, it works… so I’m guessing I have a problem with the conditional statement or the type of data its expecting.

action: badnest.boost_hot_water
data:
  entity_id: water_heater.1st_floor_thermostat_hot_water
  time_period: 30
  boost_mode: |
    [[[ if (states['water_heater.1st_floor_thermostat_hot_water'].state === "on")
      return false
    else
      return true
    ]]]

Hi @degreecy

Is it because you have three = symbols. Should this just be 2?

The [[[ denotes javascript in some specific configurations.
For templates, {% and %} are used.

Or does this code comes from a dashboard?

I’ve tried two = and even one same error

I’m just testing it in developer tools at moment with the idea to create a button card to switch on and off. If I remove the IF statement and just set boost_mode: true, it works successfully turning the hot water on.
I copied this code from another post for same integration, but cannot get it to work.

Checking the state of the entity returns on or off as a string.

You cannot use templates in the Action tool (afaik).
If you test with the template debugger, it won’t work either, as your code is javascript.

Your code will only (maybe) work in a dashboard, with the context (custom component) of the thread you copied the code from. HA doesn’t support javascript natively (afaik)

Put your actions in a script (with jinja templates, not JavaScript) then call the script from the card action.

1 Like

The original code was as follows which was for a card in a dashboard. I’ve tried this code in a button card, replacing for my entity, but I get the same error message.

type: custom:button-card
entity: water_heater.downstairs_thermostat_hot_water
show_state: true
show_name: false
tap_action:
  action: call-service
  service: badnest.boost_hot_water
  service_data:
    entity_id: water_heater.downstairs_thermostat_hot_water
    time_period: 30
    boost_mode: |
      [[[
         if (states['water_heater.downstairs_thermostat_hot_water'].state === "on")
           return false
         else
           return true
      ]]]

Thank you, will have a look into that.

As already said above, you can’t use esphome code (javascript) in HA, since esphome uses javascript which ha doesn’t recognize. Yaml code in ha is different, like

{% if states(‘sensor. water_heater.1st_floor_thermostat_hot_water’) == “on” %}
True
{%else%}
False
{%endif%}

Or, for binary template sensor i guess this should work (untested!):

{% states(‘sensor. water_heater.1st_floor_thermostat_hot_water’) == “on” %}

Both examples should work in developer tools test field.

But as said, it might be that you can’t use templates in actions at all, so you’ll have to create a script and call it, as tom suggested.

Thanks. Sorry for being a real donut here. But it should work in a custom card right or is this the same as an “Action” ? e.g. I’ve tried using this in a button card, and I get exactly the same error message.

I presume this is the only way to add a conditional input

show_name: true
show_icon: true
type: button
entity: water_heater.1st_floor_thermostat_hot_water
tap_action:
  action: perform-action
  perform_action: badnest.boost_hot_water
  data:
    entity_id: water_heater.1st_floor_thermostat_hot_water
    time_period: 30
    boost_mode: |
      {% if states(‘water_heater.1st_floor_thermostat_hot_water’) == “on” %}
        true
      {%else%}
        false
      {%endif%}
name: Hot Water
show_state: true

When button card is pressed, the error is the same i.e. invalid boolean value:

Failed to perform the action badnest/boost_hot_water. invalid boolean value {% if states(‘water_heater.1st_floor_thermostat_hot_water’) == “on” %} true {%else%} false {%endif%} for dictionary value @ data['boost_mode']

Not necessarily, templates are not supported in the actions of most cards… even custom cards that support templates in other parts of their configuration.

You need to use the method Tom described previously.

Ok thanks, will put in a script and see how I go, as probably it will make easier to set heating schedules.

Unfortunately the person who originally posted this code has not replied. However I think I just figured out why their code didn’t work for me - because he was using the custom button-card… which allows javascript… !

Why not install “custom button card” then ? It’s by far the most usable card in HA (or should i say hacs). I use it in, i guess, appr. 70% of my dashboard cards.

Yes I have done it, thank you! However I think having the script will allow me to set automations to use same function, as there is no on/off with this integration for nest. Basically you have to boost the hot water function for a given time.