Samsung TV state unknown fires automation?

Hi,

I have a Samsung TV connected via ethernet. I have setup the media_player platform samsungtv
When using the TV’s state in an automation, it triggers when the TV is in standby. Last night I tried a new automation (check below) which should fire when the TV state is on. But the TV was in standby all night and it still turned on the automation 3 times that night:

  - alias: Livingroom - TV Activity ON if Samsung TV ON
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: media_player.samsung_tv
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.livingroom_tv
        state: 'off'
    action:
      - service: switch.turn_on
        entity_id: switch.livingroom_tv

When checking the media_player.samsungtv I can see that it is Off almost all the time, but it is Unknown for a very small moment here and there (a minute or less, cannot tell more specific).

How come my automation triggers when the state is unknown? I have the trigger set to on

All help is welcome!

I’ve had piss poor luck with my samsung tv reporting anything useful at all. So much so that I disabled it from HA and instead setup a binary sensor (ping) to ping the TV every 15 seconds. If the tv responds to a ping the binary sensor is ‘on’ if no ping response the sensor is ‘off’ I then use the value of that binary sensor for my automation that need to know if the TV is on or not.

A state trigger with only to: 'on' specified will trigger whenever the state of the entity changes to ‘on’, regardless of what the state was (‘off’, ‘unknown’, etc.) just prior to that.

First, how do you know the automation was triggered? Where do you see this?

If you determine that indeed it did trigger, then you may need to look more closely at the state history of media_player.samsung_tv, because there had to be a transition to ‘on’ at some time to trigger the automation. (Actually, you should be able to see when the automation triggered, and then you can look at the state history of the media player at that time.)

You are right. For some reason it showed unknown yesterday. When checking this morning I can see that the state changed to on for less than a minute a few times this night. So the real problem here is that it does come online for a brief moment. Is it possible to check how many seconds a state has been active? When checking the history I can only see what minute it changes state, and it changes from on to off in the same minute.

@Rhavin42
I actually do both right now to test it out. And I get the exact same issue with pinging the TV as using the media_player. It will respond to the ping in standby too, once every few hours.

For me, the easiest way is to search the HA log. If you know where your home-assistant.log file is, and you’re comfortable using grep, you can do this:

grep -F 'new_state=<state media_player' home-assistant.log

You will see output like this:

2018-08-01 11:03:49 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.family_room=off; friendly_name=Family Room Chromecast, supported_features=21437 @ 2018-08-01T11:03:49.695531-05:00>, entity_id=media_player.family_room, old_state=<state media_player.family_room=unavailable; friendly_name=Family Room Chromecast, supported_features=21437 @ 2018-08-01T11:03:49.538074-05:00>>

This part will tell you what state it changed to:

new_state=<state media_player.family_room=off

This part will tell you what state it changed from:

old_state=<state media_player.family_room=unavailable

And the timestamps after the @ signs will tell you when these changes happened down to the microsecond.

Or you can use the History Statistics Sensor.

Eventually, assuming you can’t change this behavior, you’ll want to add the for: parameter to your trigger so that it will only trigger if it stays ‘on’ for some amount of time you specify.