How to combine multiple sensors and act as a switch?

My goal is to combine both a temperature and a window sensor. And of both meet certain conditions, then disable a specific device. Otherwise enable it.

I tried creating a templates switch without success.
For the start, I just used the window condition in the value_template (but temperature would follow):
But even this single condition never triggers the device on/off:

switch:
  - platform: template
    switches:
      my_combined_switch:
        value_template: "{{ is_state('binary_sensor.openclose_kitchen', 'off') }}"
        #value_template: "{{ is_state('binary_sensor.openclose_kitchen', 'off') and (states('sensor.temperature_sensor')|float > 20) }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.kitchendevice
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.kitchendevice

If you want things to act you need to write automations. Your template switch uses the state of the template to report the state of the switch - it doesn’t control it with that template.

You don’t want to create a switch. You want to create a binary sensor that you can then use in an automation to control the switch.

here is the binary sensor based on your (partial) code above:

binary_sensor:
  - platform: template
    sensors:
      my_combined_sensor:
        value_template: "{{ is_state('binary_sensor.openclose_kitchen', 'off') and (states('sensor.temperature_sensor')|float > 20) }}"
1 Like

Can I somehow achieve this in a single automation? If the binary_sensor is on, then turn my kitchendevice on. If the sensor changes to off, then turn my kitchendevice off.
Or would I have to write two automations?

You can do that in one, with either templates or a choose section.

It doesn’t gain you anything though, so I always recommend starting with two, and then learning how to merge them if you really want to.

But it’s not possible to not create the binary_sensor, but directly add this condition to an automation? Because an automation can only act on a single trigger, and here I want to combine multiple conditions?

I somehow feels wrong to create an extra binary_sensor that is only used in one (or 2) automations.

Sure you can, you can add both as triggers, and as conditions.

automation:
- trigger: 
  - platform: state
    entity_id: binary_sensor.openclose_kitchen
    to: 'off'
  - platform: numeric_state:
    entity_id: sensor.temperature_sensor
    above: 20
- condition:
  - condition: state
    entity_id: binary_sensor.openclose_kitchen
    state: 'off'
  - condition: numeric_state
    entity_id: sensor.temperature_sensor
    above: 20
- action:
  - service: switch.turn_on
    entity_id: switch.kitchendevice

As per the automation docs:

  • when either trigger becomes true
  • and both conditions are true
  • run the action
1 Like