Setting switch state based on ping

I can tell if my TV is on or off based on whether the IP is pingable…

command: ping -c 5 192.168.2.28 &> /dev/null && echo success || echo fail

Gives me a "success"or a “fail”…

how do i structure a “Command_Line” switch “command_state” to read that value?

this is my code block :

 - platform: command_line
   switches:
     tv:
       command_on: >
         curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}' "http://192.168.2.27:8080/jsonrpc?request="
       command_off: >
         curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}' "http://192.168.2.27:8080/jsonrpc?request=" && sleep 3 && curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"standby"}},"id":1}' "http://192.168.2.27:8080/jsonrpc?request=" 
       command_state: >
         ping -c 5 192.168.2.28 &> /dev/null && echo success || echo fail
       value_template: ''
       friendly_name: TV

From the command_line switch docs:

command_state (Optional): If given, this command will be run. Returning a result code 0 will indicate that the switch is on

Have you tried it with:
ping -c 5 192.168.2.28 &> /dev/null && echo 0 || echo 1

2 Likes

That might have actually worked! Thanks!

Edit : Well… it worked… kind of… as in it came on… but now it thinks it’s always on

If you just need to see if the TV is online, you can use the binary ping sensor (I use this to ping Google DNS and if its not reachable–HA knows my Internet is down)

- platform: ping
  host: 8.8.8.8

Then you can do any kind of automation to react depending on what state its in.

Automation

- alias: Turn on Orb light when Internet/Google isn't reachable
  trigger:
    - platform: state
      entity_id: binary_sensor.ping_binary_sensor
      to: 'off'
  action:
    - service: light.turn_on
      entity_id:
        - light.orb_light
      data:
        brightness: 250
        rgb_color: [190, 20, 0]

All i want to do is for the switch to know if the TV is on or off though… So i have an actual switch on the interface and not the lightning strikes…

Is there a way to do that?

The ping sensor will do exactly that. If HA can ping it, the binary sensor is on. If it can’t ping it, its off. Isnt that what you want?

Maybe i’m misunderstanding you…

I currently have this :

image

i want it like this :

image

How do i use a binary sensor as part of the command_line switch structure i pasted above to acheive that?

Hmm. I definitely didn’t realize that was what you were asking. If simply want it to be a slider, you need to customize that entity

set the assumed_state to false and you will get the slider

There’s definitely been some mis communication :slight_smile:
I’d like it to actually function… So when the TV is on, the ping responds, when the TV is off, it doesn’t.

i should be able to use that in a command_state for it to know properly whether the TV is on or not.

Are you just using this as an indicator in the HA gui to know if the TV is on or off? (You aren’t trying to control the TV from this, right?)

I’m doing both… With a command_line switch you have to enter 3 things…

command_on
command_off
command_state

i have command_on and command_off working fine… my code is in my first post…

What I need is a command_state. The answer is to use a ping command. if it pings, then the TV is on, if it doesn’t then the Tv is off.

OK. I am following. Still morning here :slight_smile:

@VDRainer was tracking. You say with his example, it thinks its always on?

Does this work any better?

ping -c 5 192.168.2.28 &> /dev/null; echo $?

Afraid not, always seen as on with this one too

image

image

Have you tried it without doing 5 pings? Like just 1 or 2?

I havn’t no… One thing i will note is that after turning the TV off, it does take around 10 - 15 seconds to stop pinging… However it should still eventually detect that the TV is off via a ping…

i’ll try with fewer pings though.

No difference… turned tv off, restarted HA and it still thinks the TV is on…

Hi @danmed, did you ever resolved your problem?

I did yeah, there is a small delay whilst the ping starts to respond, but it works…

Binary Sensor

  • platform: ping
    name: frontroomtelevision
    host: 192.168.2.230
    scan_interval: 30

Switches

  • platform: broadlink
    host: 192.168.2.51
    mac: ‘34:EA:34:58:C5:BE’
    timeout: 15
    switches:
    tv:
    friendly_name: “Livingroom TV”
    command_on: ‘JgBQAAABJZcRFQ8WETgRFREUEBURFRAVETkQOREVEDkRORE5EDkROREUEBURFRA5ERUQFREUEBYQORE5ETkQFRE5EDkRORE5EAAFOAABJU0RAA0FAAAAAAAAAAA=’
    command_off: ‘JgBQAAABJZcRFQ8WETgRFREUEBURFRAVETkQOREVEDkRORE5EDkROREUEBURFRA5ERUQFREUEBYQORE5ETkQFRE5EDkRORE5EAAFOAABJU0RAA0FAAAAAAAAAAA=’

  • platform: template
    switches:
    front_room_television_template:
    value_template: “{{ is_state(‘binary_sensor.frontroomtelevision’, ‘on’) }}”
    turn_on:
    service: switch.turn_on
    data:
    entity_id: switch.tv
    turn_off:
    service: switch.turn_off
    data:
    entity_id: switch.tv

1 Like

Thank You. I’ll try this template switch, but I have a doubt, because my power on command by broadlink RM is indeed a toggle command. Theoretically, that template will turn the tv switch on when the binary sensor state switch to on, thus updating the switch status. In the reality, I think it will shut the tv off, because of the toggle nature of the command. BTW, I’ll let you know.