Sync TV state to decide turn on or not tv

Hi, i am using this code to know tv status, its returns ON or OFF.

        binary_sensor:
  - platform: ping
    host: 192.168.1.237
    scan_interval: 10
    name: lg_tv_status

to turn on tv using broadlink IR transmitter, i use this code:

  - platform: broadlink
    host: 192.168.1.23
    mac: !secret mac_broadlink_felipe
    timeout: 15
    switches:
      # Will work on most LG TVs
      tv_lg_felipe:
        friendly_name: "TV"
        command_on: 'JgBQAAABKJIVEBUQFTQVEBUPFRAVEBUPFjMWNBUQFTQVNBY0FTQVNBYPFRAVEBU0FRAVDxUQFRAVNBU0FTQWDxU1FTQVNBY0FQAFHgABKEkVAA0FAAAAAAAAAAA'
        command_off: 'JgBQAAABKJIVEBUQFTQVEBUPFRAVEBUPFjMWNBUQFTQVNBY0FTQVNBYPFRAVEBU0FRAVDxUQFRAVNBU0FTQWDxU1FTQVNBY0FQAFHgABKEkVAA0FAAAAAAAAAAA'

I would like to sync tv status to tv switch button, so if i try turn of tv and status already marked as off, it wont send power command again…

So how will you detect the tv is on?

I do it by having a chromecast connected via USB on the tv. When that comes online I know the tv is on.

There might be other methods, detecting someone on the line in port. Who knows. Especially if the tv doesn’t respond to ping.

Actually first part of code which use ping to see if it’s on or off, showed pretty much reliable until now. I also could use CEC, as Raspberry pi is connected to my TV and both devices has hdmi cec. That’s is not my main problem right now… But, how to code using IF Then, to sync this tv status to my switch button… This would avoid I ask alexa to turn off tv, when it actually already powered off, and as consequence turning it on…