Nmap to detect running Tower PC

Hi,

I have created an easy blueprint to control the office light (Trigger on motion, depending on lux and sun = action) .
After a while the light turns off. I need a second factor that uses the running infrastructre that is on anyway, to detect the running PC.

Nmap or Ping - I went for Nmap.
I have integrated it in the afore mentioned automation. It worked fine when I set my Linux in standby (home → not_home).
Problem, when I switched off the PC the status didn’t change to “not_home”, so the light was on (home), according to the History of HASS

The Arcadyan Modem-Router of my Internet-SP, has no integration to HASS. The PC is connected to a switch, that I turn off together with the PC → no electricity. The Intel NUC (HASS) is connected to the Modem.

Is there a trick, how I teach HASS to change the status to not_home if the device is not reachable?

Any other solution is also appreciated.

Thank you.

A follow up to myself.
Weird, the (home → not_home) worked now or was it the reboot?

I threw out everything below the sun state and created a new ‘choose’ section.
Now, the light stays on all the time I am at the PC, but it doesn’t turn.off when I shutdown the PC.
I am a bit lost right now, how do I get the HASS to challenge\question\restart the automation, when I shutdown the PC?

alias: Nmap-pc-centric-02c
description: ""
triggers:
  - alias: When DesktopPC x470 changes from any state (null) to Home
    trigger: state
    entity_id: device_tracker.giga_byte_technology_31_66_9a
    from: "null"
    to: home
  - trigger: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    from: "off"
    to: "on"
conditions:
  - condition: numeric_state
    entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
    below: 37
actions:
  - choose:
      - conditions:
          - alias: Illumination from dawn (day time period).
            condition: sun
            after: sunrise
            before: sunset
            after_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 81
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
      - conditions:
          - alias: Illumination from dusk (night time period).
            condition: sun
            after: sunset
            before: sunrise
            after_offset: "-00:30:00"
            before_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 69
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
    alias: Choose light condition with sun
  - choose:
      - conditions:
          - alias: PC off AND no-motion
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: not_home
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
            state: "off"
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
      - conditions:
          - alias: PC on AND below 37 lux
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: home
          - condition: numeric_state
            entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
            below: 37
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
    alias: PC off AND no-motion | PC on AND below 37 lux
mode: restart
trace:
  stored_traces: 19

I would honestly suggest to start over from scratch and just use ping. No messing around with home & not home states for a stationary device - just a boolean which toggles to off(line) when not detected.

Well, according to HISTORY the Nmap works fine. So, I don’t want to start with another senor, plus Nmap can be used for other function as well.
Nmap is configure to scan-interval 50 seconds and 25 seconds marking a device.

Now, I am one step further. Light goes off, if PC goes to standby or off :sunglasses:

My final problem, only one of the second “choose” section does get used. The one with the red arrow, never!

Conditions are AND, so I want to check, no-motion AND not_home, if both are true, turn the light off. Why not?

YAML of just the second “choose” section:

alias: Nmap-pc-centric-02d


  - alias: PC off AND no-motion -=OR=- PC on AND below 37 lux
    choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
            state: "off"
          - alias: PC off AND no-motion
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: not_home
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
      - conditions:
          - alias: PC on AND below 37 lux
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: home
          - condition: numeric_state
            entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
            below: 37
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
          - wait_for_trigger:
              - trigger: state
                entity_id:
                  - device_tracker.giga_byte_technology_31_66_9a
                from: home
                to: not_home
            alias: Wait for trigger, PC changes from Home to Away
  - action: light.turn_off
    metadata: {}
    data:
      flash: short
      transition: 9
    target:
      entity_id: light.tradfri_weiss_1000lm_light
mode: restart
trace:
  stored_traces: 19

The whole YAML code:

alias: Nmap-pc-centric-02d
description: ""
triggers:
  - alias: When DesktopPC x470 changes from any state (null) to Home
    trigger: state
    entity_id: device_tracker.giga_byte_technology_31_66_9a
    from: "null"
    to: home
  - trigger: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    from: "off"
    to: "on"
conditions:
  - condition: numeric_state
    entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
    below: 37
actions:
  - choose:
      - conditions:
          - alias: Illumination from dawn (day time period).
            condition: sun
            after: sunrise
            before: sunset
            after_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 81
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
      - conditions:
          - alias: Illumination from dusk (night time period).
            condition: sun
            after: sunset
            before: sunrise
            after_offset: "-00:30:00"
            before_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 69
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
    alias: Choose light condition with sun
  - alias: PC off AND no-motion -=OR=- PC on AND below 37 lux
    choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
            state: "off"
          - alias: PC off AND no-motion
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: not_home
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
      - conditions:
          - alias: PC on AND below 37 lux
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: home
          - condition: numeric_state
            entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
            below: 37
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
          - wait_for_trigger:
              - trigger: state
                entity_id:
                  - device_tracker.giga_byte_technology_31_66_9a
                from: home
                to: not_home
            alias: Wait for trigger, PC changes from Home to Away
  - action: light.turn_off
    metadata: {}
    data:
      flash: short
      transition: 9
    target:
      entity_id: light.tradfri_weiss_1000lm_light
mode: restart
trace:
  stored_traces: 19

Check your automation traces to see why it picked one option over the other.

Maybe you were marked as being home - the traces will walk you through the logic at every step

That is the mean part of it, it doesn’t tell me why:
PC off AND no-motion -=OR=- PC on AND below 37 lux: No action executed

The choose section in the middle works everytime.

Click on the trace timeline

This is what I copied in on line 2, in the post above. Here as picture.
The ‘step details’ contain usually more information.

Another follow up to myself.
I don’t understand, why one of the ‘choose’ does work action: light.turn_on and the other does not work action: light.turn_off.

However, I found a “work around”. Change it from a State State to: a State AND Trigger.
Et voila, now it works.

In conclusion, the light stays on, as long as the PC is ON. The light will turn OFF, when the PC is in standby or off.
Motion, the motion sensor, does turn on and off the light as expected.

Nice side effect, I don’t need a mm-wave radiation permanently on my body, but the automation does, What I want. This function can also be used on a TV or TV-Box.

Picture of the flow chart:

The YAML code:

alias: Nmap-pc-centric-02e
description: >-
  Motion-activated light, lux, sunset/sunrise, LED brightness + Nmap.
  Illumination in conjunction of the position of the sun.
triggers:
  - alias: When the Tower PC changes from Away to Home
    trigger: state
    entity_id: device_tracker.giga_byte_technology_31_66_9a
    to: home
    from: not_home
  - alias: Detect motion
    trigger: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    from: "off"
    to: "on"
conditions:
  - alias: Room light gets below threshold
    condition: numeric_state
    entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
    below: 37
actions:
  - alias: Illumination (light) in conjunction of the position of the sun.
    choose:
      - conditions:
          - alias: Illumination from dawn +30m (day time period).
            condition: sun
            after: sunrise
            before: sunset
            after_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 81
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
      - conditions:
          - alias: Illumination from dusk -30m (night time period).
            condition: sun
            after: sunset
            before: sunrise
            after_offset: "-00:30:00"
            before_offset: "00:30:00"
        sequence:
          - data:
              brightness_pct: 69
            target:
              entity_id: light.tradfri_weiss_1000lm_light
            action: light.turn_on
  - alias: PC off AND trigger of no-motion -=OR=- PC on AND below 37 lux.
    choose:
      - conditions:
          - alias: PC off AND no-motion
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: not_home
        sequence:
          - wait_for_trigger:
              - trigger: state
                entity_id:
                  - binary_sensor.lumi_lumi_sensor_motion_aq2_motion
                from: "on"
                to: "off"
            alias: Detected to Clear
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
      - conditions:
          - alias: PC on AND below 37 lux
            condition: state
            entity_id: device_tracker.giga_byte_technology_31_66_9a
            state: home
          - condition: numeric_state
            entity_id: sensor.lumi_lumi_sensor_motion_aq2_illuminance
            below: 37
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.tradfri_weiss_1000lm_light
          - wait_for_trigger:
              - alias: Trigger waits for PC state change from Home to Away
                trigger: state
                entity_id: device_tracker.giga_byte_technology_31_66_9a
                from: home
                to: not_home
          - action: light.turn_off
            metadata: {}
            data:
              transition: 9
              flash: long
            target:
              entity_id: light.tradfri_weiss_1000lm_light
mode: restart
trace:
  stored_traces: 19

Any comment or idea is appreciated.
I hope my journey helps one or the other.