Automation question

Hi Guys,

I’d like to create an automation however I just can find the correct way of doing so:

First things first these entities are involved:

switch.tv_livingroom ( states on or off \ can turn on or off the tv)
sensor.tv_livingroom ( states on or standby)
media_player.kodi_livingroom

The “sensor.tv_livingroom” triggers a remote cec request to get the current state of my tv what I would like to do is first if the sensor changes I’d like to set the “switch.tv_livingroom” switch to match the status without sending the actual switch command so I get a consistent user interface.

  1. I’d like to trigger the switch.tv_livingroom if “media_player.kodi_livingroom” changes to playing.

and last

  1. I’d like to turn off the tv with switch.tv_livingroom if “media_player.kodi_livingroom” changes to stop however a slight delay of let’s say a minute or two would be good here.

I’ve read through the documentation but I’m struggling with the automations if anyone has the time to help that would be greatly appreciated.

Thanks

I think this should work for 1. but will depend on your set up how formatting is adjusted - hyphens to separate automations etc.

alias: Kodi Livingroom begins playing
trigger:
    - platform: state
      entity_id: media_player.kodi_livingroom
      to: 'playing'
    - platform: state
      entity_id: media_player.kodi_livingroom
      to: 'on'
      from: 'off'
action:
  - service: homeassistant.turn_on
    entity_id: switch.tv_livingroom

I am not sure how you go from an off state to playing without the TV or Kodi being already on so choose your trigger, or use both.

And this might work for the second 1. but I’m not sure, it’s a start.

alias: Kodi Livingroom stops playing
trigger:
    - platform: state
      entity_id: media_player.kodi_livingroom
      from: 'playing'
      for:
        minutes: 2
action:
  service: homeassistant.turn_off
  entity_id: switch.tv_livingroom

I don’t quite understand the sensor.tv_livingroom part you are trying to do but it’s late and I need to go to bed so probably reading comprehension failure on my part.

1 Like

Thanks a lot I’ll give that a shot, with the sensor I meant that I have a sensor which detects the state of my TV e.g. on and standby I also have a switch to turn it on or off which I wanted to match of the sensor state changes. This is what I’ve come up with which seems to work any improvements let me know:

- alias: Kodi Livingroom CEC power switch ON
  hide_entity: true
  trigger:
      - platform: state
        entity_id: sensor.tv_livingroom
        to: 'on'
        from: 'standbye'
  action:
    service: homeassistant.turn_on
    entity_id: switch.tv_livingroom

- alias: Kodi Livingroom CEC power switch off
  hide_entity: true
  trigger:
      - platform: state
        entity_id: sensor.tv_livingroom
        to: 'standbye'
        from: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: switch.tv_livingroom

One last thing is there any call to stop playback on kodi without switching off the device? Or do I need a command line toggle and call the API?

PS. if you wonder why I don’t use the HDMI-CEC component the CEC client is a remote device and accessed via ssh
Cheers

EDIT: Found it there is media/stop

All working now your automatons worked a treat.

Thanks a lot!!

Tops! I think those automations are straight adaptations from the examples and cookbooks but for other use cases. Have a look at this one and see if it looks familiar.

I don’t think it matters but instead of using homeassistant.turn_on or .turn_off for the domain in the service part of the action you might use the specific domain, - switch, scene, light, group and another handy one as you have found is media player. This opens up a few more command options when you use a device specific domain.

action:
  service: switch.turn_on
  entity_id: switch.tv_livingroom