Cannot set automation based on a state

Yes, that is normal. The UI visual editor doesn’t support templates but you can do it in YAML. I don’t use the UI but edit YAML directly.

Note that I’ve edited my above post.

Ok thanks the clarification. I did try your solution with modifying only the times for fast testing but it’s not triggering

alias: Diffuser manager
trigger:
  - alias: no motion for 30 sec
    platform: state
    entity_id:
      - binary_sensor.detecteur_mouvements_sdb_motion
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "off"
  - alias: diffuser has been on for 30 sec
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "off"
  - alias: diffuser has been off for a minute
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: "on"
condition:
  - condition: or
    conditions:
      - alias: want to switch diffuser on
        condition: trigger
        id: "on"
      - condition: and
        conditions:
          - alias: no motion for 30 sec
            condition: state
            entity_id: binary_sensor.detecteur_mouvements_sdb_motion
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 30
          - alias: diffuser on for 30 sec
            condition: state
            entity_id: switch.diffuseur_salon
            state: "on"
            for:
              hours: 0
              minutes: 15
              seconds: 30
action:
  - service: switch.turn_{{ trigger.id }}
    entity_id: switch.diffuseur_salon

At first I thought maybe I had to start & stop manually the diffuser so it can start the routine but it’s not ?

Sorry for all of this i’m kinda novice

You’ve left the final condition at 15m 30s.

1 Like

Good catch! Let me try now

Awesome it’s working great! I just have to start & stop manually the diffuser so the automation can work

Last thing : If I reboot the system I guess i’ll have to start & stop manually the diffuser again to enable the automation ?

Should I leave then another trigger to start the diffuser every hour (as I did at first) ?

Thanks a lot for your help anyway

I think the “diffuser has been off” trigger will survive a restart. Alternatively, add another trigger:

  - platform: homeassistant
    event: start
    id: "on"

Perfect, now I think i’m all set!!

I’ve added some conditions and notifications to my mobile when it’s on and off

Thanks again for your help

alias: "APPAREIL: SALON DIFFUSEUR RITUALS"
trigger:
  - alias: no motion for 15 mns
    platform: state
    entity_id:
      - binary_sensor.detecteur_mouvements_sdb_motion
    to: "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: "off"
  - alias: diffuser has been on for 15 mns
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: "off"
  - alias: diffuser has been off for an hour
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 1
      minutes: 0
      seconds: 0
    id: "on"
  - platform: homeassistant
    event: start
    id: "on"
condition:
  - condition: or
    conditions:
      - alias: want to switch diffuser on
        condition: trigger
        id: "on"
      - condition: and
        conditions:
          - alias: no motion for 15 mns
            condition: state
            entity_id: binary_sensor.detecteur_mouvements_sdb_motion
            state: "off"
            for:
              hours: 0
              minutes: 15
              seconds: 0
          - alias: diffuser on for 15 mns
            condition: state
            entity_id: switch.diffuseur_salon
            state: "on"
            for:
              hours: 0
              minutes: 15
              seconds: 0
          - condition: zone
            entity_id: device_tracker.iphone_13
            zone: zone.home
          - condition: zone
            entity_id: device_tracker.iphone_damandine
            zone: zone.home
          - condition: time
            before: "23:59:00"
            after: "08:00:00"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
action:
  - service: switch.turn_{{ trigger.id }}
    entity_id: switch.diffuseur_salon
  - if:
      - condition: trigger
        id: "on"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = ON
  - if:
      - condition: trigger
        id: "off"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = OFF
description: ""

Sorry but I have an issue with the conditions i’ve added.

condition: or
conditions:
  - condition: zone
    entity_id: device_tracker.iphone_13
    zone: zone.home
  - condition: zone
    entity_id: device_tracker.iphone_damandine
    zone: zone.home
enabled: false

If the diffuser is running and i’m leaving the house (so leaving the zone) the diffuser won’t stop as it’s not matching the condition.

How can I apply the condition only to the ON trigger and not the OFF ?

Here is my full code :

alias: "APPAREIL: SALON DIFFUSEUR RITUALS"
description: ""
trigger:
  - alias: no motion for 15 mns
    platform: state
    entity_id:
      - binary_sensor.detecteur_mouvements_sdb_motion
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "off"
  - alias: diffuser has been on for 15 mns
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "off"
  - alias: diffuser has been off for an hour
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: "on"
  - platform: homeassistant
    event: start
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: zone
        entity_id: device_tracker.iphone_13
        zone: zone.home
      - condition: zone
        entity_id: device_tracker.iphone_damandine
        zone: zone.home
    enabled: true
  - condition: or
    conditions:
      - alias: want to switch diffuser on
        condition: trigger
        id: "on"
      - condition: and
        conditions:
          - alias: no motion for 15 mns
            condition: state
            entity_id: binary_sensor.detecteur_mouvements_sdb_motion
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 30
          - alias: diffuser on for 15 mns
            condition: state
            entity_id: switch.diffuseur_salon
            state: "on"
            for:
              hours: 0
              minutes: 0
              seconds: 30
action:
  - service: switch.turn_{{ trigger.id }}
    entity_id: switch.diffuseur_salon
  - if:
      - condition: trigger
        id: "on"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = ON
  - if:
      - condition: trigger
        id: "off"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = OFF

What are you trying to achieve exactly?

You have two phones listed — how do you want them to affect the automation? Do you want them to:

  • prevent the diffuser switching on if one of them is away / both of them are away?
  • switch the diffuser off if one / both of them leaves the zone?
  • something else?

Yes, i want to :

  • prevent the diffuser switching on if both of them are away
  • switch the diffuser off if both of them are not in the house (leaving the zone)

EDIT : I also want the diffuser to start when one of the phone is entering the zone

This is becoming quite a complex automation now. The edits below achieve the first bullet point — only allowing the diffuser to switch on if at least one phone is home.

alias: "APPAREIL: SALON DIFFUSEUR RITUALS"
trigger:
  - alias: no motion for 15 mns
    platform: state
    entity_id:
      - binary_sensor.detecteur_mouvements_sdb_motion
    to: "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: "off"
  - alias: diffuser has been on for 15 mns
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: "off"
  - alias: diffuser has been off for an hour
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 1
      minutes: 0
      seconds: 0
    id: "on"
  - platform: homeassistant
    event: start
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - alias: want to switch diffuser on
            condition: trigger
            id: "on"
          - condition: or
            conditions:
              - condition: zone
                entity_id: device_tracker.iphone_13
                zone: zone.home
              - condition: zone
                entity_id: device_tracker.iphone_damandine
                zone: zone.home
      - condition: and
        conditions:
          - alias: no motion for 15 mns
            condition: state
            entity_id: binary_sensor.detecteur_mouvements_sdb_motion
            state: "off"
            for:
              hours: 0
              minutes: 15
              seconds: 0
          - alias: diffuser on for 15 mns
            condition: state
            entity_id: switch.diffuseur_salon
            state: "on"
            for:
              hours: 0
              minutes: 15
              seconds: 0
action:
  - service: switch.turn_{{ trigger.id }}
    entity_id: switch.diffuseur_salon
  - if:
      - condition: trigger
        id: "on"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = ON
  - if:
      - condition: trigger
        id: "off"
    then:
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint 🏮
    alias: NOTIFICATION WHEN DIFFUSEUR = OFF

The second and third requests are best handled with a separate automation as it’s getting too long and complex:

alias: "APPAREIL: SALON DIFFUSEUR 2"
trigger:
  - platform: zone
    entity_id: device_tracker.iphone_13
    zone: zone.home
    event: leave
    id: "off"
  - platform: zone
    entity_id: device_tracker.iphone_damandine
    zone: zone.home
    event: leave
    id: "off"
  - platform: zone
    entity_id: device_tracker.iphone_13
    zone: zone.home
    event: enter
    id: "on"
  - platform: zone
    entity_id: device_tracker.iphone_damandine
    zone: zone.home
    event: enter
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: trigger
            id: "off"
          - condition: not
            conditions:
              - condition: zone
                entity_id: device_tracker.iphone_13
                zone: zone.home
              - condition: zone
                entity_id: device_tracker.iphone_damandine
                zone: zone.home
      - condition: trigger
        id: "on"

action:
  - service: switch.turn_{{ trigger.id }}
    entity_id: switch.diffuseur_salon

If those are the only phones tracked, you can probably make this simpler, because the state of zone.home will be the number of phones at home: 0, 1 or 2.

1 Like

Ok got it! I’ll create 2 automations then if it’s easier.

Thanks for helping :slight_smile:

1 Like

Hey,

Just wanted to let you know that I managed to make everything work in all in one automation :smiley:

alias: "APPAREIL: SALON DIFFUSEUR"
description: ""
trigger:
  - alias: diffuser has been on for 15mns
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: "off"
    enabled: true
  - alias: diffuser has been off for 1 hour
    platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 1
      minutes: 0
      seconds: 0
    id: "on"
  - type: illuminance
    platform: device
    device_id: e7ada39ba908a097a83f4ee91b7e171b
    entity_id: sensor.hue_motion_sensor_1_illuminance
    domain: sensor
    id: lightoff
    alias: Luminance is below 3
    below: 3
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - platform: zone
    entity_id: device_tracker.iphone_13
    zone: zone.home
    event: enter
    id: alexin
  - platform: zone
    entity_id: device_tracker.iphone_13
    zone: zone.home
    event: leave
    id: alexout
  - platform: zone
    entity_id: device_tracker.iphone_damandine
    zone: zone.home
    event: enter
    id: amandinein
  - platform: zone
    entity_id: device_tracker.iphone_damandine
    zone: zone.home
    event: leave
    id: amandineout
condition: []
action:
  - if:
      - condition: trigger
        id: "off"
      - condition: state
        entity_id: binary_sensor.hue_motion_sensor_1_motion
        state: "off"
        for:
          hours: 0
          minutes: 15
          seconds: 0
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint 🏮
    alias: NO MOTION 15MNS = OFF
  - if:
      - condition: trigger
        id: "on"
      - condition: numeric_state
        entity_id: sensor.hue_motion_sensor_1_illuminance
        above: 3
      - condition: time
        after: "08:00:00"
        before: "23:59:00"
        weekday:
          - mon
          - tue
          - thu
          - wed
          - fri
          - sun
          - sat
        enabled: true
      - condition: and
        conditions:
          - condition: or
            conditions:
              - condition: zone
                entity_id: device_tracker.iphone_13
                zone: zone.home
              - condition: zone
                entity_id: device_tracker.iphone_damandine
                zone: zone.home
    then:
      - type: turn_on
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé 🏮
    alias: OFF FOR 1HOUR = ON
  - if:
      - condition: trigger
        id: lightoff
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint🏮
    alias: LUMINANCE BELOW 3 = OFF
  - if:
      - condition: trigger
        id: amandinein
      - condition: not
        conditions:
          - condition: zone
            entity_id: device_tracker.iphone_13
            zone: zone.home
    then:
      - type: turn_on
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé🏮
    alias: AMANDINE ARRIVES HOME
  - if:
      - condition: trigger
        id: amandineout
      - condition: not
        conditions:
          - condition: zone
            entity_id: device_tracker.iphone_13
            zone: zone.home
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint🏮
    alias: AMANDINE LEAVES HOME
  - if:
      - condition: trigger
        id: alexin
      - condition: not
        conditions:
          - condition: zone
            entity_id: device_tracker.iphone_damandine
            zone: zone.home
    then:
      - type: turn_on
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé🏮
    alias: ALEX ARRIVES HOME
  - if:
      - condition: trigger
        id: alexout
      - condition: not
        conditions:
          - condition: zone
            entity_id: device_tracker.iphone_damandine
            zone: zone.home
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Eteint🏮
    alias: ALEX LEAVES HOME
mode: single
1 Like