Run part of an automation when someone is home

Hi,

I have an automation for my washing machine that does a few things.
One of the last steps it has is to turn on a light once the washing is done. All works well and no issues.

What I want to do is have the part where the light turns on only run if someone is home (we all use the HA app on our mobiles). If nobody is home just wait till someone is and then turn on the light and finish off that part of the automation.

I have tried a few things but I can make it work so will appreciate any assistance. This the code:

alias: "Helper: Set Dishwasher Status to Unemptied"
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.smartpower_13_power
    for: "00:01:00"
    below: "0.3"
condition:
  - condition: state
    entity_id: input_select.dishwasher_status
    state: Running
action:
  - service: notify.mobile_app_mnms24ultra
    data:
      message: Dishwasher has finished its washing cycle
      title: 🏁 Dishwasher is done 🏁
      data:
        channel: OfflineDevices
        ttl: 0
        priority: high
        color: green
  - service: input_select.select_option
    data:
      option: Unemptied
    target:
      entity_id: input_select.dishwasher_status
  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5
mode: single

Thanks guys.

Use wait_for_trigger:

with the trigger being (assuming you’ve set up your home zone):

- platform: state
  entity_id: zone.home
  from: "0"

Thanks - tried a few times.
I cant get syntax, order etc right.

Also the trigger in the code above starts the action.
I need that to do the same.
I only need the last action to not happen till someone is home.

Will need help with an actual code that I can test with.

alias: "Helper: Set Dishwasher Status to Unemptied"
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.smartpower_13_power
    for: "00:01:00"
    below: "0.3"
condition:
  - condition: state
    entity_id: input_select.dishwasher_status
    state: Running
action:
  - service: notify.mobile_app_mnms24ultra
    data:
      message: Dishwasher has finished its washing cycle
      title: 🏁 Dishwasher is done 🏁
      data:
        channel: OfflineDevices
        ttl: 0
        priority: high
        color: green
  - service: input_select.select_option
    data:
      option: Unemptied
    target:
      entity_id: input_select.dishwasher_status
  - wait_for_trigger:
      - platform: state
        entity_id: zone.home
        from: "0"
  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5
mode: single

Just a note that this is also available in the visual editor, so you can try that when you are unsure about the intendations, order in the code etc. It’s in the “then do” section, “add construction block”.

1 Like

Thank you guys.
I will give it a test soon.
Appreciate the help.

This will not turn on the light if someone is home when the machine is done.
It will wait for a change from 0

1 Like

You’re right, of course. Use this instead:

  - if:
      - condition: state
        entity_id: zone.home
        state: "0"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id: zone.home
            from: "0"
  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5

That was my thought too. I think the logic for the last part needs to be:

  • if someone is home, turn on the light
  • else wait for trigger (person comes home) turn light on

I think this YAML will give you that:

  - if:
    - condition: numeric_state
      entity_id: zone.home
      above: "0"
    then:
      - service: light.turn_on
        target:
          entity_id: light.ledlight_1
        data:
          color_name: blue
          transition: 5
    else:
      - wait_for_trigger:
          - platform: state
            entity_id: zone.home
            from: "0"
      - service: light.turn_on
        target:
          entity_id: light.ledlight_1
        data:
          color_name: blue
          transition: 5

My version’s a bit more compact without repeating the action:

  • if noone is home, wait until they are
  • turn light on

Agreed. I was busy typing my post while yours was posting. And once I saw your solution I was like, “yup, that’s a much cleaner way to do that.” '-)

1 Like

Hi @Troon - I have been trying your code I keep getting this error:

Message malformed: expected str for dictionary value @ data['action'][2]['if'][0]['state']

The I added in is (I will paste the whole Action block):

action:
  - service: notify.mobile_app_mnms24ultra
    data:
      message: Dishwasher has finished its washing cycle
      title: 🏁 Dishwasher is done 🏁
      data:
        channel: OfflineDevices
        ttl: 0
        priority: high
        color: green
  - service: input_select.select_option
    data:
      option: Unemptied
    target:
      entity_id: input_select.dishwasher_status
  - if:
      - condition: state
        entity_id: zone.home
        state: 0
    then:
      - wait_for_trigger:
          - platform: state
            entity_id: zone.home
            from: "0"
  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5

Any idea why is giving me that error?

The code posted by @pkscout works fine.

I didn’t think it would cause an issue. But it seems it does.
You need "" around the 0.

Make is:

  - if:
      - condition: state
        entity_id: zone.home
        state: "0"

Huh, learn something every day with HA. Follow @Hellis81’s suggestion, corrected in my earlier post.

@MnM — it is worth trying to understand what these error messages are telling you. They may look obscure, but that’s actually a pretty clear message.

Message malformed:
expected str for dictionary value @
data['action'][2]['if'][0]['state']

It’s saying “look at your action block, third (because we count from 0) item, at the if block, at the first (and only) item inside it, then at the state: I was expecting a string value (str) but didn’t get one”.

If you’re unsure what a string is, and why 0 isn’t one, read up on the various data types here (linked from Developer Tools / Template in your system):

https://jinja.palletsprojects.com/en/latest/templates/#literals

1 Like

Thank you all for your assistance.
I have implemented it (using Troon’s code) and it seems to be working ok. Need to test and see what will happen when nobody is home, but when everyone is home is works fine).
Also thanks for explaining the error to me. It is good to know what they mean.

I am trying to get it to get this script to the next level now.
Basically I have 2 similar scripts (one for dishwasher an one for washing machine) using the same led light. One turns the led lights blue the other green.

All is good when the scripts run individually. However, if both run the same time the last script to finish will override the color of the led light. I am looking now to have another condition where the last script to finish will check the status helper of the other script and if the status = unemptied then make the led light RGB.

The 2 status helper used in the 2 scripts are:

input_select.dishwasher_status
and
input_select.washing_machine_status

So the dishwasher script will check the input_select.washing_machine_status to see if is = unemptied. If it is then will turn on RGB on the led light (or an effect that I have defined via ESPHome on my led light), but still use the conditions made above - if user not home do nothing till user home.
If input_select.washing_machine_status != unemptied then proceed with script with the solution above.

I did try myself to have a go, but I think I am still too green to make it happen, so I am in need of your assistance again :slight_smile:

It’s always helpful to post what didn’t work and the entire YAML. It makes it easier for folks to see all the logic and have the names of your actual sensors and everything. That said, let’s give this a go. At the end of the dishwasher YAML put (you should fix the indents below so they match with your YAML - another reason to post the entire YAML):

  - if:
      - condition: state
        entity_id: input_select.washing_machine_status
        state: "unemptied"
    then: 
      - service: light.turn_on
        target:
          entity_id: light.ledlight_1
        data:
          color_name: RGB
          transition: 5
    else:
      - service: light.turn_on
        target:
          entity_id: light.ledlight_1
        data:
          color_name: blue
          transition: 5

Then something similar at the end of the washing machine one but checking the other input select and setting the right color(s).

@pkscout Apologies - yes i should have posted the code. here it is, below.
So the code you have posted will not invalidate the home/not home portion of the script below? Will just run in addition to it?

alias: "Helper: Set Dishwasher Status to Unemptied"
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.smartpower_13_power
    for: "00:01:00"
    below: "0.3"
condition:
  - condition: state
    entity_id: input_select.dishwasher_status
    state: Running
action:
  - service: notify.mobile_app_mnms24ultra
    data:
      message: Dishwasher has finished its washing cycle
      title: 🏁 Dishwasher is done 🏁
      data:
        channel: OfflineDevices
        ttl: 0
        priority: high
        color: green
  - service: input_select.select_option
    data:
      option: Unemptied
    target:
      entity_id: input_select.dishwasher_status
  - if:
      - condition: state
        entity_id: zone.home
        state: "0"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id: zone.home
            from: "0"
  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5
mode: single

If you replace this:

  - service: light.turn_on
    target:
      entity_id: light.ledlight_1
    data:
      color_name: blue
      transition: 5

with the code I provided, I think it will do what you want. The only question mark for me is the RGB color. Is that a color the LED recognizes that causes it to rotate colors?

@pkscout - thank you for your help with this. I tested it in the weekend and it run as I wanted it to.