Templating a Sensor that shows State of Entity 5 seconds ago

I am trying to get my bedroom tv to turn off automatically when my chromecast turns off. Unfortunately my older tv will turn on via CEC, not not off. Anyway, I have successfully done this with my Broadlink and automations:

- alias: Master Bed Cast Ended
  trigger:
  - platform: state
    entity_id: media_player.bedroom_tv
    to: "off"
  condition: []
  action:
  - service: script.turn_on
    entity_id: script.master_tv_power

My problem now is that sometimes at night the chromecast will drop off the wifi, or do an update, or something else that makes home assistant read it as ‘unavailable’. When it comes back, the state returns to ‘off’, and the automation is triggered. Again, unfortunately, this tv only has a power toggle command, not discreet commands like I use on my main setup. The problem is a wake up in the middle of the night with my tv on and bright in my face.
What I think I need is a template sensor that reflects the state of the chromecast as it was a few seconds ago, so that my automation can add a condition, something like:

  condition:
  - condition: state
    entity_id: sensor.bedroom_tv_recently
    state_not: 'unavailable'

This way, if the chromecast changes from ‘playing’ to ‘off’, or anything else to ‘off’, the action will run. But if it just changed from ‘unavailable’ to ‘off’, the condition will not be met and the action will be skipped. I think this is the best way to accomplish this, if it’s possible. The only other idea I had was on the trigger, maybe there’s some kind of ‘from_not’ tag i could add? I use ‘to’ and ‘from’ based on the current needs, but I am not aware of ways to use more than 1 or if there is a ‘not’ function for those.
Any ideas on other solutions are welcome as well, thank you!

You can use the Trigger state object in your condition.

condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }}"

Oh wow hello. Trying that out now. Thank you! It’s always easier than I make it

Also one other tip. Scripts are services, so you could do this in the action instead:

- service: script.master_tv_power

I copied in your exact code, and it works perfectly, so thank you so much. I was unaware that the != would work as a ‘does not equal’. That’s one of those things that I will be implementing in other places

Oh yeah, duh, thank you. I should do that across my lovalace as well, its getting a bit cumbersome even after I split everything up https://github.com/ericksonian/HomeAssistant

No prob. Make sure to mark VDRainer’s post as the solution btw. If others with a similar question come across your post it helps them quickly determine what the answer is :slightly_smiling_face:

Thank you. I marked the solution, and a few simple find/replace commands and all my script.turn_on services are gone. According to my github commit, that cleaned out 230 lines from my config and lovelace!

You should be aware that this changes the way the script is called.

    - service: script.myscript

Calls a script and requires the caller to wait for it to complete or it hits a timer/delay

    - service: script.turn_on
      entity_id: script.myscript

This starts the script without waiting and the caller continues immediately.

The way of calling scripts is due to be changed very shortly so this may have been jumping the gun a bit.

@Tediore (for reference)