Command line switch state how to set with device state?

Hello I have a switch like this:

switch:
  - platform: command_line
    switches:
      kodi_tv:
        command_on: '/config/kodi_on.sh'
        command_off: '/config/kodi_off.sh'
        command_state:

and a Device called media_player.vpnpi the device can have different states (idle,playing,off) How can I set the command state that the switch is seen as on as long as the device is not off?

I’m not sure what you mean. The command_state is the script to run to get the device state but you already have it in HA. What are you trying to achieve?

I’am starting Kodi and the tv on my pi with a script but the state of the switch in homeassistant turns to off aggain after a few seconds. I’d like for the switch to have the correct state because otherwise mycroftAi(voice command) tells me that kodi is already off and doesn’t turn it off. I somehow need to configure the switch state to be correct.

I see why you might be confused by this. I have Kodi configured in home assistant but my script kodi_off.sh kills the kodi process so I can’t start it via kodi integration.

Sorry I am new to Home assistant and don’t really know how to set the command_state I thought it would tell Home Assistant if the switch is off or on right now.

Just curious if the Kodi integration https://www.home-assistant.io/integrations/kodi/ works for you?
That will provide a basic set of status and functions without much work, see the integration for details.

For command_line switch, it expects a state value for the command_state, usually 0/1 or true/false.

If you still want to use the command_line switch, you need another command for command_state that returns 0/1, true/false.

Welcome to the forum :smiley:

Thank you, yes it works for me but I want to kill kodi so I need this switch. So how can I ask home assistant in which state the device media_player.vpnpi is ? or how can I determine if kodi is running? I saw something in other posts about pinging but I can’t ping a port because I am running home assitant in a docker-container.

I tried something now but the switch is always on I have a script that gives 0 or 1 for kodi up or down. How am I supposed to give it to home assistant? just echo something else ?

Remove the command_state and use the value_template.

value_template: "{{ states('media_player.vpnpi') != 'off' }}"

EDIT: Sorry, my bad. The command_state must be there or you loose the toggle.
But it doesn’ matter what it returns if you use the value_template.

thanks I just had it like this :slight_smile:

value_template: “{{ not is_state(‘media_player.vpnpi’, ‘off’) }}”

should be the same but my problem now is that when I kill kodi the entity media_player.vpnpi just stays on idle I don’t know if it is forever.
I ll try this night how long.

Here’s a little script that works for me as command_state:

#!/bin/bash
if grep 'ON' /tmp/testswitch; then
    exit 0
else
    exit 1
fi

Thanks a lot i’am using homeassistant in docker so I connected to the host via ssh no clue if that is the smartest option but i might try your approach

I appreciate the help:
i found that this works for my setup because I use docker:

#!/bin/sh
ssh [email protected] 'ps -C kodi >/dev/null && exit 0 || exit 1'
exit  $?
1 Like