Why is this condition (automation) not working?

I’m trying to disable manual control of radiators.
So if the switch is turned on when the climate entity is in idle then it should turn off the switch. And vice versa.

Kitchen (kok) and livingroom (is the same room that’s why I need the if else).

alias: "Element av om man styr manuellt "
description: Styrning av smart plug manuellt rättas till.
trigger:
  - platform: state
    entity_id:
      - switch.element_david_switch
      - switch.element_matilda_switch
      - switch.element_sovrum_switch
      - switch.element_kok_switch
      - switch.element_vardagsrum_switch
    from: "off"
    to: "on"
    id: off-on
  - platform: state
    entity_id:
      - switch.element_david_switch
      - switch.element_matilda_switch
      - switch.element_sovrum_switch
      - switch.element_kok_switch
      - switch.element_vardagsrum_switch
    from: "on"
    to: "off"
    id: on-off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - off-on
          - condition: template
            value_template: |-
              {%- if "kok" in trigger.entity_id -%}
                {{ state_attr("climate.vardagsrum", 'hvac_action') == "idle" }}
              {%- else -%}
                {{ state_attr("climate." ~ trigger.entity_id.replace("switch","").replace("_","").replace(".element",""), 'hvac_action') == "idle" }} 
              {%- endif -%}
        sequence:
          - service: switch.turn_off
            target:
              entity_id: "{{ trigger.entity_id }}"
            data: {}
          - service: tts.cloud_say
            metadata: {}
            data:
              entity_id: media_player.hela_huset
              message: >-
                Det är snällt att ni vill hjälpa mig med att styra värmen i
                lägenheten, men jag klarar det bra själv. Tack ändå.
              language: sv-SE
              cache: false
      - conditions:
          - condition: trigger
            id:
              - on-off
          - condition: template
            value_template: |-
              {% if "kok" in trigger.entity_id %}
                {{ state_attr("climate.vardagsrum", 'hvac_action') == "heating" }}
              {% else %}
                {{ state_attr("climate." ~ trigger.entity_id.replace("switch","").replace("_","").replace(".element",""), 'hvac_action') == "heating" }} 
              {% endif %}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: "{{ trigger.entity_id }}"
            data: {}
          - service: tts.speak
            metadata: {}
            data:
              cache: true
              media_player_entity_id: media_player.hela_huset
              message: >-
                Det är snällt att ni vill hjälpa mig med att styra värmen i
                lägenheten, men jag klarar det bra själv. Tack ändå.
              language: sv-SE
mode: single

When I test the template in developer tools I get this:

{% set trigger = "switch.element_vardagsrum_switch" %}

{% if "kok" in trigger %}
  {{ state_attr("climate.vardagsrum", 'hvac_action') == "idle" }}
{% else %}
  {{ state_attr("climate." ~ trigger.replace("switch","").replace("_","").replace(".element",""), 'hvac_action') == "idle" }} 
{% endif %}

Climate is:
{{ state_attr("climate." ~ trigger.replace("switch","").replace("_","").replace(".element",""), 'hvac_action') }} 

image

Why does it allow me to turn on the switch even though the climate entity is in idle?

The condition in the left lane is just a condition I tested with but it removed.

So it’s not the yaml that is the issue.
I just noticed when I turn on the heater of a generic thermostat then it will change the mode from idle to heating.
And that is kind of an issue.

Here the room temperature is above the target temperature and it just accepts that you can turn on the heater.

And because the room temperature is above the target then I guess it will remain on “forever”.

Does the configuration of the Generic Thermostat currently use the initial_hvac_mode option?

If it does, what is its value?

That just sets the mode when you reload yaml or reboot.
There is no discrepancy, it switches the attribute to make sure there is discrepancy. So it works as intended I guess.

I noticed something though. When I moved the condition from the chose to the “real” conditions the automation works.
Probably this is because the climate entity does not have time to change when the condition is in conditions but it has time when it’s in the actions.

So I tried to make them variables in the hope this will be read first.

alias: "Element av om man styr manuellt "
description: Styrning av smart plug manuellt rättas till.
variables:
  david: "{{ state_attr('climate.david', 'hvac_action') }}"
  vardagsrum: "{{ state_attr('climate.vardagsrum', 'hvac_action') }}"
  sovrum: "{{ state_attr('climate.sovrum', 'hvac_action') }}"
  matilda: "{{ state_attr('climate.matilda', 'hvac_action') }}"
trigger:
  - platform: state
    entity_id:
      - switch.element_david_switch
      - switch.element_matilda_switch
      - switch.element_sovrum_switch
      - switch.element_kok_switch
      - switch.element_vardagsrum_switch
    from: "off"
    to: "on"
    id: off-on
  - platform: state
    entity_id:
      - switch.element_david_switch
      - switch.element_matilda_switch
      - switch.element_sovrum_switch
      - switch.element_kok_switch
      - switch.element_vardagsrum_switch
    from: "on"
    to: "off"
    id: on-off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - off-on
          - condition: template
            value_template: |-
              {%- if "kok" in trigger.entity_id -%}
                {{ vardagsrum == "idle" }}
              {%- elif "vardagsrum" in trigger.entity_id -%}
                {{ vardagsrum == "idle"}} 
              {%- elif "david" in trigger.entity_id -%}
                {{ david == "idle"}} 
              {%- elif "matilda" in trigger.entity_id -%}
                {{ matilda == "idle"}} 
              {%- elif "sovrum" in trigger.entity_id -%}
                {{ sovrum == "idle"}} 
              {%- endif -%}
        sequence:
          - service: switch.turn_off
            target:
              entity_id: "{{ trigger.entity_id }}"
            data: {}
          - service: tts.cloud_say
            metadata: {}
            data:
              entity_id: media_player.hela_huset
              message: >-
                Det är snällt att ni vill hjälpa mig med att styra värmen i
                lägenheten, men jag klarar det bra själv. Tack ändå.
              language: sv-SE
              cache: false
      - conditions:
          - condition: trigger
            id:
              - on-off
          - condition: template
            value_template: |-
              {%- if "kok" in trigger.entity_id -%}
                {{ vardagsrum == "heating" }}
              {%- elif "vardagsrum" in trigger.entity_id -%}
                {{ vardagsrum == "heating"}} 
              {%- elif "david" in trigger.entity_id -%}
                {{ david == "heating"}} 
              {%- elif "matilda" in trigger.entity_id -%}
                {{ matilda == "heating"}} 
              {%- elif "sovrum" in trigger.entity_id -%}
                {{ sovrum == "heating"}} 
              {%- endif -%}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: "{{ trigger.entity_id }}"
            data: {}
          - service: tts.speak
            metadata: {}
            data:
              cache: true
              media_player_entity_id: media_player.hela_huset
              message: >-
                Det är snällt att ni vill hjälpa mig med att styra värmen i
                lägenheten, men jag klarar det bra själv. Tack ändå.
              language: sv-SE

mode: single

It works on two of the radiators but not the other. I’m probably milliseconds late in reading the attribute

I guess I could create sensors that is “delayed” in it’s state by only switching the state when it has been for: 5 seconds or something.
But that just feels so wrong.
I might have to go for the more static approach.

If the room temperature is in range switch off. The issue is that I need to hard code the cold and hot tolerance since that is not an attribute