State: 'not_home'

I have a script to play a sound when there is motion outside my door.
This works perfect:

# Kattvakt HOME
  - alias: 'Kattvakt_home'
    trigger:
      platform: state
      entity_id: binary_sensor.movement
      to: 'on'
    condition:
      condition: state
      entity_id: group.fgh
      state: 'home'
    action:
      service: shell_command.play_moo

I wanted to have another sound played when group.fgh is NOT_HOME so I added this:
#Kattvakt NOT_HOME
- alias: ‘Kattvakt’
trigger:
platform: state
entity_id: binary_sensor.movement
to: ‘on’
condition:
condition: state
entity_id: group.fgh
state: ‘not_home’
action:
service: shell_command.play_bark

But ofcourse it wasnt as easy as change to state: ‘not_home’??

I would have been hapy to use only one script for this with some kind of elseif function but im not into programing.

Please format the second automation like the first. It seems ok but it’s hard to tell if not formatted correctly.

Are you sure that group.fgh’s state is only ever 'home' and 'not_home', or could it be something else sometimes?

You could do something like this:

# Kattvakt HOME
  - alias: 'Kattvakt_home'
    trigger:
      platform: state
      entity_id: binary_sensor.movement
      to: 'on'
    action:
      service_template: >
        {% if is_state('group.fgh', 'home') %}
          shell_command.play_moo
        {% else %}
          shell_command.play_bark
        {% endif %}

Thank you very much!

I tried to format the second automation like the first using the button but that didnt work.

Your solution looks exactly as I imagine it should and could look, if only I was a litle bit into programing! :slight_smile: Anyway… now it turns out movement doesnt trigger the script at all? None of he shellcommands are beeing executed. Only if I activates the script by hand from within Home Assistant. (And then the if/else template works).

If manually triggering the automation works, and it causes the right shell command to execute, then the issue is the trigger. Try going to the States page and manually changing the state of binary_sensor.movement to 'off' then to 'on', and see if that causes the automation to trigger (and a shell command to run.) If so, then the problem is the binary_sensor isn’t changing as you think. Or maybe your automation is turned off.

Ok. Looks like I was just too fast to trie if it worked. The binary_sensor.movement didnt turn to off before I activatade the actual device. Everything seems perfectly right now! Guess I have to live with the random delay between the physical movement and actual statechange in Home Assistant.

Thank you very much for helping me out!