home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.63k stars 30.39k forks source link

Template Switch if-then conditions not working #126938

Open hssmith76 opened 2 weeks ago

hssmith76 commented 2 weeks ago

The problem

Created a very simple Template switch through the GUI which evaluates an entity state before setting the power of a heater (scene activate) - it does not evaluate the conditions correctly always failing the condition. I created the exact same as an automation and it works correctly. Both YAMLs included for reference.

What version of Home Assistant Core has the issue?

2024.9.3

What was the last working version of Home Assistant Core?

Dont know

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Template Swtich

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

YAML from switch - does not evaluate condition correctly

if:
  - condition: state
    entity_id: input_boolean.rhosneigr_living_heater_enabled
    state: "on"
then:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.rhosneigr_living_heater_high
else:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.rhosneigr_living_heater_low

YAML from Automation - this works

if:
  - condition: state
    entity_id: input_boolean.rhosneigr_living_heater_enabled
    state: "on"
then:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.rhosneigr_living_heater_high
else:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.rhosneigr_living_heater_low

Anything in the logs that might be useful for us?

No response

Additional information

No response

hacscred commented 2 weeks ago

The probability of you doing something wrong is overwhelmingly large considering that if-conditions in YAML work fine for me (and many thousands of others).

I recommend you create a minimal self-contained example reproducing the issue. Something which either defines the inputs you refer to or which doesn't even need those.

This level of issue is better suited on the forums, until you can actually prove there is a problem.

hssmith76 commented 2 weeks ago

I agree with you which is why I did exactly this and I have recreated it from scratch just now again. I have a template switch with nothing other than the if/then element above as a building block within the actions if turned on - it never evaluates as true. The exact same content is present in an automation (and nothing else), this correctly evaluates the difference in the entity and sets it either as high or low accordingly. This is the first time I have used a template switch, I have had no issues with such building blocks as part of automations - works exactly as expected, therefore I was surprised that it just would not work with such simple criteria in the switch.

hacscred commented 2 weeks ago

Try the same with the system_log integration instead of your scene.

hssmith76 commented 2 weeks ago

I have done exactly this - same result. I even tried it with a different boolean entity to see if something strange there, same result. One thing that might relate to why I am seeing this and everyone else does not see it - for my switch I have no value template as I have no way to test whether it is on or off - this is as per the documentation says, but might be rarer. It is also not linked to a device, not really sure what this means in this situation? Literally the only configuration on the switch is actions if turned on/off.

Switch YAML - never evaluates as true

if:

Automation YAML - works as expected, writing 'Automation high' when true:

if:

hssmith76 commented 2 weeks ago

BTW - thank you, any support gratefully received

hacscred commented 2 weeks ago

Also use code tags such that the code is rendered as code and not as bullet points.

You can further simplify replacing the condition with a condition always returning true, like "{{ true }}" without the quotes, I believe. (Or one returning always false, depending on what you want.)

I wouldn't really call your template switch a template, because there is no templating happening.

I never specify metadata myself, so that might be special.

I am also slightly confused what doesn't work. I thought the problem was that the Automation did work, but the YAML didn't.

For the YAML you should probably specify a completely valid fragment. For the automation, a screenshot would probably be best. I never use the GUI to specify anything related to dynamic behavior.