Run a sensor scan or script (something?) at specific time everyday

Is it possible to have a sensor only scan at a specific time of day? Or should I be looking into something other than a sensor for this?

I have a Wemo Insight that has a “today_kwh” property, but the default wemo component in home assistant creates a sensor that scans every 10 minutes and it shows a histogram with lots of colors from when it took a reading every 10 minutes.

sc1

The way the Wemo works is it will keep adding up the usage and “today_kwh” will keep getting larger until midnight when it resets back to 0 again.

I want to track my daily power usage and calculate an average, ideally displaying something useful in the UI instead of the raindbow histogram that I have now. I thought about using a sensor with a scan interval but if I start my home assistant instance up at 8pm, I still need to take the reading between 11-12, not 8pm the next day.

Still a bit new to home assistant, especially doing custom stuff. I wrote with a basic python script that does a SOAP request to the Wemo which returns back the insight stats. I just need a way to run this at specific time every day and show the results in the UI. My apologies, Python isn’t my strongest language so please feel free to offer any suggestions/improvements.

/config/wemo.py:

import requests, xmltodict, sys
def getWemoInightStat(ip, stat):
    url = "http://%s:49153/upnp/control/insight1" % (ip)
    headers = {'content-type': 'text/xml','SOAPAction':'"urn:Belkin:service:insight:1#GetInsightParams"'}
    body = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetInsightParams xmlns:u="urn:Belkin:service:insight:1"/></s:Body></s:Envelope>'
    response = requests.post(url,data=body,headers=headers)
    doc = xmltodict.parse(response.content)
    result = doc['s:Envelope']['s:Body']['u:GetInsightParamsResponse']['InsightParams']
    parts = result.split('|')
    stats = {}
    stats['state'] = parts[0]
    stats['onfor'] = parts[2]
    stats['currentmw'] = parts[7]
    stats['todaymw'] = parts[8]
    stats['totalmw'] = parts[9]
    print(stats[stat])
if __name__ == "__main__":
    ip = sys.argv[1]
    stat = sys.argv[2]
    getWemoInightStat(ip, stat)

Assuming the script does what you want, you can simply create an automation that runs the script at say 11:55pm, by calling the script.turn_on service and specifying your script name as the entity_id.

See the example in the link below. You probably won’t need to include the data section, unless you wrote your script to receive variables.

Yes I could write an automation that runs the script, but I don’t think that will show up in the Home Assistant UI like a sensor would. I could use an automation like you suggestand run a modified version of the script which writes to a file, I suppose.

Then the problem is how to get that data in a human readable presentation in the UI. That is what I am struggling with now.

I wrote the script thinking I could use the code to build some kind of custom component or sensor that would allow me to essentially show a daily reading in a graph or chart or something that is more presentable then the image I included in my original post.

The problem is I have the sensor output, and I can even set the scan_interval on a sensor, but I essentially want a sensor that scans at a specific time everyday and I have yet to figure out how to accomplish this, as simple as it may seem.

1 Like

Hi, have you got a solution on this? I am exactly looking for a way to do the same, i.e. have a sensor only working on specific time of the day.

Thanks!