Home Assistant

How To Integrate Devices In Home Assistant Using Ping

Home Assistant offers a binary sensor integration that allows a ping to be sent to any device on your network.

This provides us with a really great way to monitor the presence of a particular device on our network.

The most obvious way we could use this integration is to sense if a person is home by pinging their mobile phone or smart watch. If you are away and your device is not connected to your network, Home Assistant knows that you are away.

However there is another really great way we can utilise the ping integration. We can use it to sense the state of a dumb switch.

Bitmoji Image

Make a dumb switch smart

Recently I wrote a post that describes a clever method for including a dumb light switch in Home Assistant.

This is useful if you want the main non-smart light switch in a room to turn off a bunch of smart lamps as well as the main light.

How does it work?

I would definitely recommend checking out the post as it covers the whole setup process in detail.

However to summarize the concept, we basically write an automation in Home Assistant which monitors the state of a smart bulb installed in the main light fitting.

When the power is cut to this smart bulb in the event of turning the light off using the main dumb switch, Home Assistant can react when it senses that the state of the bulb becomes unavailable.

We can use this state change to fire an automation that turns the lamps and TV off in the room. This gives our dumb light switch pseudo-smart switch capability!

super

State monitor

The original article describes how to implement this technique using Tasmota and MQTT, however it is also possible to implement it for any device that uses its own unique IP address on the network.

Therefore this article expands upon the original article and introduces the idea of monitoring a device by means of using the ping binary sensor integration rather than directly monitoring the device status.

Benefits of ping

This gives us two main benefits. Firstly we can expand the range of supported devices to any device with a unique IP address. Secondly the response time for turning off a device becomes much quicker.

When a Tasmota device is powered down, Home Assistant will not update the state until the MQTT heartbeat timeout. At the time of writing it is not possible to change this in the Tasmota configuration and by default the time is quite long.

It can take up to a minute or two before Home Assistant recognizes that the power has been cut to the smart bulb that we have connected to our dumb switch, resulting in a long delay before the rest of our devices react to the switch press.

The ping integration

A way to speed this up considerably is by using the ping integration. We simply check if the device is present by pinging it. If the device is powered down by the dumb switch, Home Assistant can detect it quicker by the absence of the return ping.

What is ping?

Ping is a network utility that allows a type of message to be sent across a computer network from one device to another with the intention of checking reachability.

A ping message is always bounced back to the device sending the message. Both the reachability and delay time can be measured. If the sending device does not receive a response within a specified timeout period, the device is considered to be offline or unreachable.

All major operating systems have a simple ping function build in. From either the Mac/Linux Terminal or Windows command line, you can ping a device on your network.

ping 192.168.1.88

This will send a series of ping messages to the specified IP address and print the round trip time of the message. The following image shows the output from pinging a smart light bulb directly from my Home Assistant server terminal.

If we try to ping a device that is offline, the ping application will allow a certain time for the return message. If it is not received then the device is deemed to be unreachable.

The time that it takes for a device to be determined unreachable is considerably quicker using ping than it is using the Tasmota MQTT integration.

This means we can use ping to considerably improve the time that it takes our lamps to respond to our main light being powered down by the dumb light switch!

Bitmoji Image

Adding to Home Assistant

Ping is implemented in Home Assistant as a binary sensor integration. Therefore should be added to the binary_sensor portion of the configuration.yaml file.

Binary sensor integration

If you do not already have the binary sensor integration, you can go ahead and add it.

binary_sensor:

Otherwise just add the code beneath the existing binary sensor integration code. Don’t forget that YAML is very sensitive to formatting, therefore you need to maintain the correct tabs/spacing.

The Home Assistant website gives a detailed explanation of what is required for ping. As a minimum you must specify the platform for the binary sensor as ping. You must also specify an IP address for the host.

In my case I would like to monitor the smart bulb in my light fixture with its IP address 192.168.1.126.

binary_sensor:
  - platform: ping
    host: 192.168.1.126

We can also optionally specify a name for this instance. I will give it a name relating to the bulb I am monitoring to make it easier to identify in Home Assistant.

binary_sensor:
  - platform: ping
    host: 192.168.1.126
    name: ping_lohasone

Next we can specify the interval time using the scan_interval attribute. This is the time in seconds that we wish to poll our device.

binary_sensor:
  - platform: ping
    host: 192.168.1.126
    name: ping_lohasone
    scan_interval: 1

Setting a value of 1 second for the scan_interval time will give us the quickest response time. However we should be careful not to swamp our network with too many instances of ping using quick polling intervals.

We can further improve our response time by reducing the number of packets sent from the default of 5 down to 1.

binary_sensor:
  - platform: ping
    host: 192.168.1.126
    name: ping_lohasone
    scan_interval: 1
    count: 1

Ping state

Now that we have our instance of ping configured using the binary sensor integration, we will be able to monitor it’s state.

Go ahead and click on developer tools, then click the states tab and enter “binary” into the filter entity search box. You should find your new ping sensor in the list.

The ping instance will either have the state “off” or “on” depending on whether Home Assistant is receiving the ping messages within the timeout period.

In this example the state of this ping instance will mirror the state of the smart bulb in the main light fixture, which is connected to the dumb switch. Therefore it’s state is equal to the “state” of the dumb light switch!

Now we can create a script or an automation that responds based upon the state of our ping instance, which is also the state of our dumb switch!

Bitmoji Image

Add to the automation

I will not cover all of the details of the automation as it is already covered in my post about making a dumb light switch smart. I would recommend you read it first.

Let’s first recap on the original automation that uses the MQTT state. This automation turns the light entity treelamp on and off when the main light is switched on and off using the dumb switch.

automation:
   - alias 'link main light to lamp on'
     trigger:
        platform: state
        entity_id: light.lohasone
        to: 'on'
      action:
        service: light.turn_on
        entity_id: light.treelamp
   - alias 'link main light to lamp off'
     trigger:
        platform: state
        entity_id: light.lohasone
        to: 'unavailable'
      action:
        service: light.turn_off
        entity_id: light.treelamp

We are interested in changing the trigger of this automation from the MQTT state of the smart bulb to the state of the ping binary sensor. Therefore we can go ahead and replace the relevant entity_id attributes.

We also need to change the to attribute from “unavailable” to “off” as this is the state that the ping binary sensor will adopt when our dumb switch is off.

automation:
   - alias 'link main light to lamp on'
     trigger:
        platform: state
        entity_id: binary_sensor.ping_lohasone
        to: 'on'
      action:
        service: light.turn_on
        entity_id: light.treelamp
   - alias 'link main light to lamp off'
     trigger:
        platform: state
        entity_id: binary_sensor.ping_lohasone
        to: 'off'
      action:
        service: light.turn_off
        entity_id: light.treelamp

Thats it! Our light entity treelamp will now turn on and off using the dumb switch. I found this method to be much faster than using the MQTT state so would highly recommend using it.

I hope that you found this tutorial useful and I am always interested to know what you thought so I can improve my content. Please go ahead and take a moment to leave me a comment below!

quality content
back to top