Set state on event

Hey & Happy Holidays! Sorry for this very simple question, but I’m clearly to stupid for home assistant. :slight_smile:

I’m trying to build an automation to turn of my TV. I wrote a JavaScript to turn off the TV, that was the easy part. Now I need to configure a switch. Here is what I did so far:

switch:
  platform: command_line
  switches:
    tv:
      friendly_name: Fernseher
      command_off: "/usr/bin/node /home/max/lgremote/turn_off.js"

Works as expected, nothing to see here. The only problem: The switch needs to figure out when the TV is on.

So far I tried:
command_state: "ping -c 1 -w 1 192.168.7.37"
This is supposed to ping the TVs IP. Doesn’t seem to get updated ever.

value_template: "{{ is_state('device_tracker.9c80dfd389f8', 'home') }}"
My device tracker tracks the TV okayish, I hoped this would just set the state of the switch to the state of the device. It doesn’t.

So now I need to add an automation:

automation 2:
  alias: Set TV switch
  trigger:
    - platform: state
      entity_id: device_tracker.9c80dfd389f8
  action:

So what do I need to do to set the state of the switch to the value of the device tracker? Is there a better solution? Where find I a good reference for home assistant? Please help!

Hi, I was wondering if you ever solved this. I have the exact same situation/problem.

Cheers, hutchinsane_

Here is what I ended up doing:

automation 2:
  alias: Set TV switch
  hide_entity: True
  trigger:
    - platform: state
      entity_id: device_tracker.9c80dfd389f8
      state: 'home'
  action:
    service: homeassistant.turn_on
    entity_id: switch.tv

There must be a much easier solution, but this is the best I came up with.

Wouldn’t that also toggle the switch? ie send a turn on signal if the device was already turned on?

I don’t thnik so but I don’t know for sure.

The way I did it now is create a dummy switch that gets turned on when the tracker tells it my TV is on, then group the dummy switch together with the real switch, because otherwise mine would turn my tv off as soon as it got tracked :slight_smile:

may be use a template switch?

switch:
  - platform: command_line
    switches:
      tv_cl:
        friendly_name: Fernseher an
        command_on: "/usr/bin/node /home/max/lgremote/turn_on.js"
        command_off: "/usr/bin/node /home/max/lgremote/turn_off.js"
  - platform: template
    switches:
      tv:
        value_template: "{{ states.device_tracker.9c80dfd389f8.state == 'home' }}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.tv_cl
        turn_off:
          service: switch.turn_off
          entity_id: switch.tv_cl

maybe this will work, too:

switch:
  - platform: command_line
    switches:
      tv:
        friendly_name: Fernseher an
        command_on: "/usr/bin/node /home/max/lgremote/turn_on.js"
        command_off: "/usr/bin/node /home/max/lgremote/turn_off.js"
        value_template: '{{ states.device_tracker.9c80dfd389f8.state == 'home' }}'
        friendly_name: Fernseher

I’m pretty sure I tried something along these lines, but it didn’t work: it wouldn’t update the current state of the switch. I think the value_template never got triggered.

not sure if you still need this. but this is how you get the tv state