Configure a command line toggle switch - motion / webcam

Hi there :slight_smile:

I’am trying for hours to configure a toggle switch that turns motion on/off (and therefor the webcam) and returns the actual state of motion.

My motion sensor looks like this (it only return on/off):

sensor:
  - platform: command_line
    name: motion
    friendly_name: Kamera Software
    command: service motion status | sed '3q;d' | grep -o -P '(?<=\().*(?=\))' | sed 's/running/on/;s/start/on/;s/dead/off/;s/stop/off/'
    scan_interval: 1

My normal camera on/off switch (the one with the lightning symbol):

 switch:
   - platform: command_line
     switches:
       camera:
         friendly_name: Kamera
         command_on: 'sudo service motion start'
         command_off: 'sudo service motion stop'

My template toggle switch:

  - platform: template
    switches:
      camera2:
        friendly_name: "Kamera 2"
        value_template: "{{ is_state('switch.camera_toggle', 'sensor.motion.state', 'on') }}"
        turn_on:
          service: switch.toggle
          entity_id: switch.camera
        turn_off:
          service: switch.toggle
          entity_id: switch.camera

The problem is: The template toggle switch toggles always back to off when I click it, the state of the sensor and the normal switch are behaving normally.

Any help would be highly appreciated :slight_smile:

value_template: "{{ is_state('switch.camera_toggle', 'sensor.motion.state', 'on') }}"
The above looks wrong to me I think you can only have …
value_template: "{{ is_state('switch.camera_toggle', 'on') }}"

So you will probably have to do an ‘if’ construct

Also if you use toggle you don’t need turn off and turn on - toggle turns off or on depending on the current state - having said all that I’ve never done a switch template so could be completely wrong :slight_smile:

Thank you for the answer :slight_smile:

Look at the example in the docs: https://home-assistant.io/components/switch.template/
They also have something like me: value_template: "{{ is_state_attr('switch.blind_toggle', 'sensor_state', 'on') }}"

Now also my simple command line switches stopped working :frowning: something is just wrong with these… For example:

switch:
  - platform: command_line
switches:
  camera:
    friendly_name: Kamera
    command_on: 'sudo service motion start && sudo hub-ctrl -h 0 -P 2 -p 1 && echo "1" > /tmp/usb_state'
    command_off: 'sudo service motion stop && sudo hub-ctrl -h 0 -P 2 -p 0 && echo "0" > /tmp/usb_state'
  usb:
    friendly_name: Fan
    command_on: 'sudo hub-ctrl -h 0 -P 2 -p 1 && echo "1" > /tmp/usb_state'
    command_off: 'sudo hub-ctrl -h 0 -P 2 -p 0 && echo "0" > /tmp/usb_state'

The lightning never turns yellow, but they still do their job somehow…