Automation when anyone (>=1 of n) or no one is in a room

Make a template sensor for each room like this:

template:
  - binary_sensor:
      - name: "Office Occupied"
        state: >
          {{ "office" in [
            states('sensor.peter_iphone_ble'),
            states('sensor.alfred_iphone_ble'),
            states('sensor.alfred_watch_ble'),
            states('sensor.jane_iphone_ble') ] }}
        delay_on: 5
        delay_off: 30

Use them as triggers or conditions in your automations.

2 Likes

Thanks a bunch @tom_l very much appreciated. I knew there was a better way, just could not find my way through to it.

I’m trying this out with an old Fitbit tracker but not successful so far. Not sure where I go wrong

  • Installed firmware on the ESP
  • ESP seems to be looking for BLE signals
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5828
entry 0x400806ac
Pre-Setup Free Mem: 206732
Setup network
Connecting to WiFi SSID 'xxxxxxxx'............................................................192.168.2.247
Firmware:     esp32
Version:      v3.2.4
WiFi BSSID:   74:AC:B9:5C:EA:49 (channel=11 rssi=-63)
IP address:   192.168.2.247
DNS address:  192.168.2.1
Hostname:     espresense-office
Room:         Office
Mqtt server:  192.168.2.97:1883
Max Distance: 4.50
PIR Sensor:   disabled
Radar Sensor: disabled
I2C Bus 1:    sda=21 scl=22
Query:        
Include:      
Exclude:      
Known Macs:   
Count Ids:    
Post-Setup Free Mem: 140012

1 Motion | Pir: no Radar: no
1 Advert | iBeacon
1 New    | 74acb95cea4b | 74acb95cea4b                                              -70dBm 
1 New    | 56c9863b66d2 | sd:0xfe9f                                                 -82dBm 
1 New    | 66c0c53027ef | apple:1002:6-12                                           -94dBm 0200
1 New    | 454545a6505f | sd:0xfe9f                                                 -83dBm 
1 Config | iBeacon:e5ca1ade-f007-ba11-0000-000000000000-252-9261 to {"id":"node:office","name":"Office"}
  • I saw the device_id of the Fitbit
  • Created the following sensor of the Fitbit
sensor:
  - platform: mqtt_room
    device_id: "sd:0x180a"
    name: 'Fitbit Thomas'
    state_topic: 'espresence/devices/sd:0x180a'
    timeout: 10
    away_timeout: 120

Is there an issue with MQTT topics? Anything else I need to watch out for?

I created the templates, now how do I use them as a trigger in automation? Thanks.

They are binary sensors. So a state trigger would be best.

trigger:
  - platform: state
    entity_id: binary_sensor.office_occupled
    from: 'off'
    to: 'on' # when the office is occupied

My problem is that binary_sensor.office_occupied is not showing although I already created sensor_office.yaml and I already restarted HA, I’m missing something…

Not

template:
  - sensor:

File /config/sensor_escritorio.yaml

template:
  - binary_sensor:
      - name: "Escritorio_ocupado"
        state: >
          {{ "escritorio" in [
            states('sensor.applewatchmorcegolas_ble'),
            states('sensor.iphoneMorcegolas_ble') ] }}
        delay_on: 5
        delay_off: 30

How are you including that file?

It might be the problem, I don’t :confused:
I think I need to add it to configuration.yaml

This is what I have:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include mysensors.yaml

Yes that is the problem. You have not included the template: integration at all in your configuration.yaml file.

You have to be very careful how you do it too. It is not structured like other integrations.

Can you help me doing that? I’m a starter here. I have this file and then repeat for all off them

There are lots of ways of doing it depending on the structure you want, have a read of this topic, but ignore all the failed attempts at the beginning, so from about here onwards: https://community.home-assistant.io/t/splitting-config-for-template/328636/44?u=tom_l

That’s a lot to read and assimilate :confused:
What’s the easiest way of doing this? Can’t I add all the templates in one file instead of creating a bunch of files with a template for each room?

In the thread you sent my you have a lot of files:

  • templates.yaml
  • template_sensors.yaml
  • template_binary_sensors.yaml

Help me breaking this out please.

First I add to configuration.yaml:

template: !include templates.yaml

In templates.yaml I just need to put this because I don’t have template_sensors:

- binary_sensor: !include template_binary_sensors.yaml

And in template_binary_sensors.yaml what do I need to put?

Yes you can do that. Then in the binary sensors file only put the part starting with - name:...

- name: "Office Occupied"
  state: >
    {{ "office" in [
      states('sensor.peter_iphone_ble'),
      states('sensor.alfred_iphone_ble'),
      states('sensor.alfred_watch_ble'),
      states('sensor.jane_iphone_ble') ] }}
  delay_on: 5
  delay_off: 30

- name: "some other room"
  state: > etc...

template_binary_sensors.yaml:

template:
  - binary_sensor:
      - name: "Escritorio_ocupado"
        state: >
          {{ "escritorio" in [
            states('sensor.applewatchmorcegolas_ble'),
            states('sensor.iphoneMorcegolas_ble') ] }}
        delay_on: 5
        delay_off: 30
      - name: "Quarto_ocupado"
        state: >
          {{ "quarto" in [
            states('sensor.applewatchmorcegolas_ble'),
            states('sensor.iphoneMorcegolas_ble') ] }}
        delay_on: 5
        delay_off: 30

Could be this?

No, look at the example in my post above.

You already have template: and - binary_sensor: in the include structure if you put it in the file as well you will double up on both of them and get nothing but config errors.

It will resolve to something like this;

template:
  - binary_sensor:
      template:
        - binary_sensor:
            - name: etc...

You don’t want that.

I got it, thanks! Let my try it to see if it will work.

@tom_l it worked, many thanks for helping out.

Just one more little question, the automation I’m trying to make work, but I need to use entity instead of device, but in Automations/Actions I can’t find one for entities.

This is what I have for devices:

alias: Test
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.quarto_ocupado
    from: "off"
    to: "on"
condition: []
action:
  - type: turn_on
    device_id: b7b8fae214399f40e749a6cf7aebfdf5
    entity_id: light.luz_switch_1
    domain: light
mode: single

If I remove device_id it says it’s malformed

I got it, I have to use service :wink:

1 Like
action:
  - service: light.turn_on
    target:
      entity_id: light.luz_switch_1