Reading a sensor for automation

Post what you got, seems like a comment is getting brought into the template output.

Iā€™m not sure how to properly put the code in the template checker

So honestly I canā€™t say.

Iā€™m such a noob, a eager one but a noob none the less.

I mean, post it here.

Nevermind found some typos, try this:

sensor:
  - platform: template
    sensors:
      next_game:
        value_template: >
          {% set game_time_string = states('sensor.browns') %}
          {% set game_channel = state_attr('sensor.browns','Channel') %}
          {% set current_time_string = states('time.date__time') %}
          {% set five_minutes = 5 * 60 %}
          {% set game_time = as_timestamp(strptime(game_time_string,'%Y/%m/%d %H:%M:%S')) - five_minutes  %}
          {% set current_time = as_timestamp(strptime(current_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' }}

lol, Oh sorry :slight_smile:

 - platform: template
    sensors:
      next_game:
        value_template: >
          {% set game_time_string = states('sensor.browns') %}
          
          {% set game_channel = state_attr('sensor.browns','channel') %}
          {% set current_time_string = states('time.date_time') %}
        
          {% set five_minutes = 5 * 60 %}
          
          {% set game_time = as_timestamp(strptime(game_time_string,'%m/%d/%y %I:%M %p')) - five_minutes  %}
          {% set current_time = as_timestamp(strptime(game_time_string,'%m/%d/%y %I:%M %p')) %}
          {% set game_end = game_time + five_minutes + 3 * 60 * 60 %}
          {{ game_channel.lower() if game_time <= current_time <= game_end else 'off' }}

There should be 2 underscores here unless the component changed. Between date__time

Ok, I was thinking my sensors name is sensor.browns so I tried that too but both ways werenā€™t working.

Ill try your updated code now. TY

I donā€™t know what you named the sensor. You gotta find it on your states page and use the correct sensor name. The problem with the template was a typo in the set current_time line, had nothing to do with with what your talking about.

Yes, I noticed that too, it had a double _ character. Or is that supposed to be there?

Its still unavailable for some reason.

paste this into the template editor

{% set game_time_string = states('sensor.browns') %}
{{ game_time_string }}

then this:

{% set game_channel = state_attr('sensor.browns','Channel') %}
{{ game_channel }}

then this:

{% set current_time_string = states('time.date__time') %}
{{ current_time_string }}

Take a screenshot of each output. Do not place all 3 in the editor at once.

yea, it is the date__time

Ok, gotta find out what the correct entity_id name is then. Take a pic of your states page and filter it using
ā€˜sensor.dā€™

That should filter out stuff to only see the date_time sensor.

Damn, Iā€™m not home now. And I canā€™t see the entity Iā€™d colum on my phone in the states menu,

This is all I can see.

I want you to know I appreciate your help. Iā€™m confident if I keep pushing myself one day I will be able to pay it forward and be a contributing part of this thriving community

No problem. So, the issue is a typoā€¦ again. My b.

Try this:

{% set current_time_string = states('sensor.date__time') %}
{{ current_time_string }}

Then use this as your template:

sensor:
  - platform: template
    sensors:
      next_game:
        value_template: >
          {% set game_time_string = states('sensor.browns') %}
          {% set game_channel = state_attr('sensor.browns','Channel') %}
          {% set current_time_string = states('sensor.date__time') %}
          {% set five_minutes = 5 * 60 %}
          {% set game_time = as_timestamp(strptime(game_time_string,'%Y/%m/%d %H:%M:%S')) - five_minutes  %}
          {% set current_time = as_timestamp(strptime(current_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' }}

Sorry to sound stupid, but where do I put this?

just check that inside the template checker

Im sorry I havenā€™t posted sooner, work has been crazy this week.

Ok that passes good in the template.

but im still getting unknown for the states view.

I wonder if it would help to show you my brown.py file and how the information is being processed. Ill post below. I wonder if its just the date__time format causing the sensor to not read it properly.

@ReneTode was so very kind in setting this up for me. maybe he has a idea?

truly ty to the both of you, I canā€™t wait until sunday to see this in action lol

###########################################################################################
# #
# Rene Tode ( [email protected] ) #
# #
# 2018/10/07 Germany #
# #
# #
# an app to that creates a sensor out of data collected from #
# Browns Schedule | Cleveland Browns - clevelandbrowns.com #
# #
###########################################################################################

import appdaemon.plugins.hass.hassapi as hass
import datetime
import time

import requests
from socket import timeout
from bs4 import BeautifulSoup

class browns(hass.Hass):

  def initialize(self):
    #################################################################
    # when initialising the sensor needs to be imported             #
    # but we need to run the same code again to get the next values #
    # thats why i only start the call_back from here                #
    #################################################################
    self.get_values(self)        

  def get_values(self,kwargs):
    #################################################################
    # first we set some values, this could be done in the yaml      #
    # but this app is specialized and will only work for this       #
    # webpage, so why bother                                        #
    #################################################################
    self.url = "https://www.clevelandbrowns.com/schedule/"
    self.sensorname = "sensor.browns"
    self.friendly_name = "Next game from Cleveland Browns"
    next_game_time = None
    #################################################################
    # now we read the webpage                                       #
    #################################################################
    try:
      response = requests.get(self.url,timeout=10)
    except:
      self.log("i couldnt read the browns schedule page")
      return
    page = response.content
    #################################################################
    # now that we got the webpage we make the data readable         #
    #################################################################
    soup = BeautifulSoup(page, "html.parser")
    #################################################################
    # in the google chrome console we are going down the tree from  #
    # body. every time an indention is visible we add the next      #
    # element. untill we see main, which contains a lot of section  #
    # elements. nextSibling makes us go to the next element on the  #
    # same level. untill we reach the table containing the schedule #
    # cards. some invisible empty siblings make that we need more   #
    # rimes nextSibling then the amount of sections                 #
    #################################################################   
    cards_table = soup.body.div.main.section.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling
    #################################################################
    # to see if we got the right data we log it. uncomment when     #
    # you expect that the webpage is changed                        #
    #self.log(cards_table)                                          #
    #################################################################
    #################################################################
    # now we find the first card inside the table                   #
    #################################################################
    first_card = cards_table.div.div
    #################################################################
    # the first card is the title card containing "regular season"  #
    # now we are going to loop to the cards following that first 1  #
    #################################################################
    for schedule_card in first_card.find_next_siblings():
        #############################################################
        # lets find the date we want out of the card                #
        #############################################################
        try:
            game_start = schedule_card.div["data-gametime"]
        except:
            #########################################################
            # there is no date found in this card (probably an add) #
            #########################################################
            game_start = ""
        #############################################################
        # if we find a date, then we need to translate the date to  #
        # a time we can compare. in this case we find a date like   #
        # like this 2018-09-09T17:00:00Z which is %Y-%m-%dT%H:%M:%S #
        # (python datetime lib docs tell us that)                   #
        #############################################################
        if game_start != "":
            game_time = datetime.datetime.strptime(game_start,"%Y-%m-%dT%H:%M:%SZ") + datetime.timedelta(hours= -4)
            #########################################################
            # find out if this date is in the future                #
            #########################################################
            if game_time > datetime.datetime.now():
                #####################################################
                # check if we didnt find one before, when not set it#
                #####################################################
                if next_game_time == None:
                    next_game_time = game_time
                    #################################################
                    # now that we know that this is the next game   #
                    # lets also lookup the opponent in the card     #
                    # it will make a nice attribute for the sensor  #
                    # to remove all whitespace we use strip()       #
                    # again we can find that by looking at the      #
                    # google chrome console                         #
                    #################################################
                    opponent = schedule_card.div.div.nextSibling.nextSibling.p.nextSibling.nextSibling.string.strip()
                    #################################################
                    # and we want to find the channel that it will  #
                    # be on.                                        #
                    #################################################
                    channel = schedule_card.div.div.nextSibling.nextSibling.div.nextSibling.nextSibling.div.div.span.nextSibling.nextSibling.string.strip()
    #################################################################
    # now we got all data we need but the date isnt what we need    #
    # we translate that again to the timeformat we want to see      #
    # for the HA sensor                                             #
    #################################################################
    next_game_str = next_game_time.strftime("%m/%d/%y %I:%M %p")
    #################################################################
    # now we got all info we need and we can create a sensor.       #
    # the first time that the code is run it will create a warning  #
    # that the sensor doesnt exist. if we see that in the log we    #
    # know that the sensor is created.                              #
    #################################################################
    self.set_state(self.sensorname, state = next_game_str, attributes = {"friendly_name": self.friendly_name,"Opponent": opponent,"Channel": channel})
    #################################################################
    # now al we need to do is make sure that the sensor stays up to #
    # date. we could check the webpage every minute, but that would #
    # be unneccesary traffic. we dont know exactly when the webpage #
    # is updated, we need to use a short time after the game, but   #
    # we dont want it to be too long                                #
    # if the sensor isnt up to date, just check the page, restart   #
    # the app and or change the extra time we now add               #
    #################################################################
    update_time = next_game_time + datetime.timedelta(hours= 4)
    #################################################################
    # so we got a time that we want to update the sensor. so we run #
    # this code again at that time.                                 #
    #################################################################
    self.run_at(self.get_values,update_time)

Ok, Iā€™m guessing it doesnā€™t contain seconds. Try this then in the editor:

{% set current_time_string = states('sensor.date__time') %}
{% set current_time = as_timestamp(strptime(current_time_string,'%Y-%m-%d %H:%M')) %}
{{ current_time }}

it now says ā€œNoneā€