Reading a sensor for automation

Hello, I use Hassos

I have a sensor that was programmed by a very smart member in appdeamon. It reads the schedule from the cleveland browns web site and lets me know when the next game is on, the time and the channel it will air on.
also the opponent that we face.

everything seems to be working from the awesome sensor.

Now I need to know if anybody has any ideas on how to extract the information from the sensor so I can set automations (turn the tv to the specific station a few minutes before kick off and run some already configured automations before the game.

the sensor is sensor.browns

thanks for any ideas!

Can you post the output of the sensor the way it is coming in to HA?

Like this?

No. From the states page. Like this:

sorry about that.

2018/10/14 17:00:00	
friendly_name: Next game from Cleveland Browns
Opponent: Los Angeles Chargers
Channel: CBS

You can access the channel & opponent the way you would any attribute:

states.sensor.next_game_from_cleveland_browns.attributes.Opponent

states.sensor.next_game_from_cleveland_browns.attributes.Channel

I assumed the sensor was called “sensor.next_game_from_cleveland_browns” and I’m not sure if you need to use all lower case in the attributes key or not. If it doesn’t work as posted then try that.

The date & time is a little trickier since it doesn’t have a standard key:value pair like the other two.

I tried to do that with another custom component and I could never work it out.

If you do figure out a way to pull that value then please let me know. And if I figure it out then I’ll post it back here.

Sensor.browns but I can add that!

Thanks very much, I will be sure to let you know if I get it all working

Another question: what is the actual state of the sensor right now? is it the date & time as you show in your screenshot?

Yes, it showed the date and time as state

well, then. that makes it easy.

just use ‘states.sensor.browns.state’ and that will return the date time string.

You will likely have to convert that to a real timestamp if you’re going to use it in an automation as, say, a reminder.

something like:

{{ as_timestamp(states.sensor.browns.state)| timestamp_custom(’%-I:%M %p on %-m-%-d-%Y’) }}

Hopefully (I think) that should convert the date time string to, as an your example, “5:00 pm on 10-14-2018”

Never mind the above. That only converts the string to a different form.

I suck at time conversions.

yea, im going to have to try to correct the time the game is actually on at 1:00pm

Ok I got the correct time!

Now to see if I can use the data for automation

Thanks for your help!

Ok, I have been trying to get this going all evening.

If anybody can point me in the right direction I would appreciate it.

Ill start from the top.

I have a sensor sensor.browns

The State Attribute is Date & Time
additional attributes are: Opponent & Channel

10/14/18 01:00 PM	
friendly_name: Next game from Cleveland Browns
Opponent: Los Angeles Chargers
Channel: CBS

Ok, I hope that I can have my harmony hub change to that station 15 minutes before game time in a automation.

Here’s my scripts for harmony with the possible channels that maybe needed.
abc:
alias: ABC
sequence:
- service: remote.send_command
entity_id: remote.harmony_hub
data:
device: ‘56583809’
command: ‘0’
- service: remote.send_command
entity_id: remote.harmony_hub
data:
device: ‘56583809’
command: ‘3’
- service: remote.send_command
entity_id: remote.harmony_hub
data:
device: ‘56583809’
command: ‘3’
- service: remote.send_command
entity_id: remote.harmony_hub
data:
device: ‘56583809’
command: Select
- service: script.turn_off
entity_id: script.abc
- service: script.turn_off
entity_id: script.abc

fox:
  alias: FOX
  sequence:
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '0'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '6'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '2'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: Select
  - service: script.turn_off
    entity_id: script.fox
  - service: script.turn_off
    entity_id: script.fox

cbs:
  alias: CBS
  sequence:
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '0'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '2'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: '7'
  - service: remote.send_command
    entity_id: remote.harmony_hub
    data:
      device: '56583809'
      command: Select
  - service: script.turn_off
    entity_id: script.cbs
  - service: script.turn_off
    entity_id: script.cbs

nbc:
alias: NBC
sequence:

  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘0’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘2’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘1’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: Select
  • service: script.turn_off
    entity_id: script.nbc
  • service: script.turn_off
    entity_id: script.nbc

nfl_network:
alias: NFL Network
sequence:

  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘1’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘5’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: ‘4’
  • service: remote.send_command
    entity_id: remote.harmony_hub
    data:
    device: ‘56583809’
    command: Select
  • service: script.turn_off
    entity_id: script.nfl_network

also, it would be cool to have another automation run 5 minutes before game time.

browns_football:
  alias: Browns Foot Ball Time
  sequence:
    - delay: '00:00:05'
    - service: script.turn_on
      entity_id: script.kitchen_loop
    - service: script.turn_on
      entity_id: script.nfl_tts
    - delay: '00:00:23'
    - service: script.turn_on
      entity_id: script.browns_lights
    - delay: '00:00:05'
    - service: script.turn_on
      entity_id: script.here_we_go
    - delay: '00:00:10'
    - service: script.turn_on
      entity_id: script.browns_football_time_tts
    - service: script.turn_on
      entity_id: script.volume_up
    - service: script.turn_on
      entity_id: script.volume_up

Is this even possible to do?

So upon further reading, it seems like I will need to make a template sensor to pull and name the attributes I’m looking for so I can set the automation to create actions based on those attributes. Is that correct?

If I understand you correctly, you want to turn the game on 5 minutes before game time?

So if that’s the case, you need to add the time sensor to your config:

sensor:
  - platform: time_date
    display_options:
      - 'date_time'

You could go with just the time module, but then the automation could fire every day. Best to go with the date_time module to include today into the mix.

Next you’ll need to make a template sensor. Now typically you’d have to make an on/off for the time of the event and a sensor for which channel. But we can combine them into a single sensor that displays off when off, and channel when on.

sensor:
  - platform: template
    sensors:
      next_game:
        value_template: >
          {% set game_time_string = states('sensor.next_game_from_cleveland_browns') %}
          # You may need to change Channel string to lowercase.
          {% set game_channel = state_attr('sensor.next_game_from_cleveland_browns','Channel') %}
          {% set current_time_string = states('time.date__time') %}
          # the next 3 variables are in seconds.
          {% set five_minutes = 5 * 60 %}
          # set time for 5 minutes before game.
          {% set game_time = as_timestamp(strptime(game_time_string,'%Y/%m/%d %H:%M:%S')) - five_minutes  %}
          {% set current_time = as_timestamp(strptime(game_time_string,'%Y-%m-%d %H:%M:%S')) %}
          {% set game_end = game_time + five_minutes + 3 * 60 * 60 %}
          {{ game_channel.lower() if game_time <= current_time <= game_end else 'off' }}

So this sensor will be equal to the lowercase channel name for 3 hours and 5 minutes, starting 5 minutes before the game. All other times, it will have a state of off.

Then with this automation:

- alias: Turn on the Game
  trigger:
    - platform: state
      entity_id: sensor.next_game
  condition:
    - condition: template
      value_template: "{{ trigger.to_state.state != 'off' }}"
  action:
    - service_template: "script.{{ trigger.to_state.state }}"

Thank You! You are a HUGE HELP!

Will this automation know to use my script to change the channel accordingly?

1 Like

Do you know what would cause the unknown state?

A typo probably, place each line of the template into the template checker and verify the output is correct.

I’m getting closer. I guess it’s better then unknown lol