Best way to turn on switch based on true/false conditions

Hello, newbye here
I would like to control my heating system based on Better Thermostat states
I want a zigbee switch to turn on if any of the BT entity is in heating state
This block

{{ (state_attr('climate.bt_cucina','hvac_action')) =='heating'}}
{{ (state_attr('climate.bt_soggiorno','hvac_action')) =='heating'}}
{{ (state_attr('climate.bt_davide','hvac_action')) =='heating'}}
{{ (state_attr('climate.bt_matrimoniale','hvac_action')) =='heating'}}

returns true/false for each state attribute hvac_action of each entity
If any of them is true, I want my switch to be turned on

Where should I look for?
Thanks
Pietro

alias: example
trigger:
  - platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide', 'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count > 0 }}
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.your_switch
2 Likes

You just need to use or:

{{ 
  (state_attr('climate.bt_cucina','hvac_action')) =='heating' or
  (state_attr('climate.bt_soggiorno','hvac_action')) =='heating' or
  (state_attr('climate.bt_davide','hvac_action')) =='heating' or
  (state_attr('climate.bt_matrimoniale','hvac_action')) =='heating'
}}
1 Like

Thanks. I must use another one to turn off if list | count = 0, right?

Or simply create one automation with two triggers.

alias: example
trigger:
  - id: 'on'
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide', 'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count > 0 }}
  - id: 'off'
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide', 'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count == 0 }}
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.your_switch
1 Like

Thanks again
I am trying to use a wifi thermostat (climate.casa) instead of a switch and set its target temperature to low/high (15/25 °C) values when a given trigger is fired
I tried to modify your code like below, but it doesn’t seem to work
Where is my error?
P

alias: AccCaldaia
description: Accendi caldaia
trigger:
  - id: "25"
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide',
      'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count > 0 }}
  - id: "15"
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide',
      'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count == 0 }}
condition: []
action:
  - service: climate.set_temperature
    target:
      entity_id: climate.casa
    data:
      temperature: "{{ trigger.id }}"

Probably:

temperature: "{{ trigger.id|int(15) }}"

Thnx
Tried to modify as your suggestion and under traces I find
Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘id’

if you run it manually then there won’t an id - because the trigger never happened.

1 Like

Now that you have replaced the switch with a climate entity, the automation’s Template Triggers don’t appear to be the neatest way of triggering the automation.

I suggest you consider using this version:

alias: AccCaldaia
description: Accendi caldaia
trigger:
  - platform: state
    entity_id:
      - climate.bt_cucina
      - climate.bt_soggiorno
      - climate.bt_davide
      - climate.bt_matrimoniale
    attribute: hvac_action
    to:
      - heating
      - idle
condition: []
action:
  - service: climate.set_temperature
    target:
      entity_id: climate.casa
    data:
      temperature: >
        {{ iif(trigger.to_state.attributes.hvac_action == 'heating', 25, 15) }}
1 Like

Thanks but now I have another error…

Did you manually run the automation to test it? You can’t do that because the trigger variable won’t be defined.

If you want to test the automation, go into the developer tools → states and find your climate entity, and then you can change the hvac_action attribute from idle to heating or vice-versa.

1 Like

You are right.

Update
With the latest code I see the climate.casa is turned on off when any of the BT instances changes state.
This is not as I like it to work.
Climate.casa should be risen if any of the BT instances is in heating state and lowered when all BT are in idle state.

With the latest code if a single BT instance reach target temperature then climate.casa is set to 15C not considering the state of the others…
Thanks again

I can confirm that this one seems ok

I tried to rise/lower the temperature of the single BT instances and if all go to idle the climate.casa is set to 15C.
If just one (or more) are in heating then the climate.casa is set to 25C

alias: AccCaldaia
description: Accendi caldaia
trigger:
  - id: "25"
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide',
      'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count > 0 }}
  - id: "15"
    platform: template
    value_template: >
      {{ ['climate.bt_cucina', 'climate.bt_soggiorno', 'climate.bt_davide',
      'climate.bt_matrimoniale']
        | select('is_state_attr', 'hvac_action', 'heating')
        | list | count == 0 }}
condition: []
action:
  - service: climate.set_temperature
    target:
      entity_id: climate.casa
    data:
      temperature: "{{ trigger.id|int(15) }}"

For future reference, it’s the custom of this community to assign the Solution tag to the first post that provides the answer/solution to the original question/problem.

In this case, you had originally asked for this:

The solution was immediately provided to you here:

You then changed your requirements, asking for to also turn off the switch and I also immediately provided a solution here:

Then you changed your requirements yet again and asked for it to set the temperature of a climate entity. You modified my second example and ultimately chose to mark your own post as the Solution, despite the fact it’s completely based on a suggested solution to a previous requirement.

It’s super easy to make it work the way you want by simply adding a condition. Anyway, good luck solving other problems in the future.

1 Like

You are right, Sorry.
I modified the solution tag.
I didn’t mean to attribute to me the solution.
Just it was the adapted solution to my needs and it worked, so I thought to sign it as solution.
As I said I just started to understand the HA code.
And I don’t know the logics behind it.
I discovered that I could set the temperature of a Tuya thermostat via automation, so my switch is not needed.
So my questions were modified.
I made a mess, again sorry.
Thanks again for your precious help.

1 Like