Better python script examples

I would love to see some better/more-complete python script examples. Maybe I’m missing something, but the one in the docs is just too basic to be useful as a basis for using the python scripting feature. Are there some “test scripts” somewhere that I should look at?

1 Like

Hi Eric, did you look at the other examples here on the forum? There are quite a few good ideas and examples.

I did, and yes, some of them are useful. I was hoping for more, but maybe it’s too soon after the release of python scripting.

I hope more people will add their scripts for others to learn from and replicate. I am specially looking for a script to combine multiple device trackers into one.

I’m not a programmer but I think if you look at the example scripts it should be fairly simple to do that.

Call a script when the state of any tracker changes, have it do it’s logic (determine whether you want it to trigger home or not), then create a new meta device_tracker sensor with the status you want.

I look forward to seeing more examples because I am not a coder but I can follow along / copy and paste if I see what someone else has done.

This was another simple one I made similar to the plant one I posted:

Counts through all the lights, and a list of switches I defined, and returns an input_boolean ‘on’ if any lights are on and ‘off’ if all lights are off. Also returns a count of how many total are on as an attribute.

Simple but it makes triggering/conditioning automations a lot simpler

2 Likes

That is exactly what I want…a meta tracker for every individual that takes data from individual trackers (Owntracks, iOS app, Router, etc.) and uses the most recent information from the individual trackers to update the meta tracker.

I am not sure how to proceed. I will look at your example and try something.

I took a crack at it, it appears to be working. Will make a more detailed post after I test it:

The automation to call it:

- alias: "Update Brad's Meta Tracker"
  trigger:
    platform: state
    entity_id: device_tracker.ios, device_tracker.owntracks, device_tracker.ping
  action:
    service: python_script.meta_device_tracker
    data_template:
      entity_id: '{{trigger.entity_id}}'

In my case, I only want ‘not_home’ signals from GPS platforms, but ‘home’ from all others

6 Likes

This is beautiful and almost exactly what I had in mind.

I can modify the home and not_home logic, but the part that I am really after is creating a meta tracker that can take inputs from multiple trackers. I think, we will also need some delay for the router based trackers and then we can use it for both home/away. I have my presence automations here. Basically, for Owntracks and router based tracker, I only update state if the new state stays for few minutes (7 in my case).

I am also thinking that instead of hard-coding the metatrackerName, we should pass it as a parameter so that the same script can be used for multiple individuals.

Or you could probably make a HA group (non-View) that has the trackers you are interested in, in it. Then read the entities in the group. There are a lot of ways it can be done. Once yall are done, post your scripts to the examples page so other people picking up the python_scripts can benefit from what you have learned.

1 Like

@arsaboo I don’t think you can put a delay in a python_script, but you could handle that with your trigger

Thank you for this! I’m trying it out now, but will have to do some tweaking since I got reported as not home and then home again in the middle of the night by my bluetooth tracker… But that will be an easy fix I think!

You can now use time.sleep()

1 Like

I get this error when i try your script:

2017-11-14 14:19:05 ERROR (Thread-19) [homeassistant.components.python_script.meta_device_tracker.py] Error executing script: Can't convert 'NoneType' object to str implicitly
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/python_script.py", line 165, in execute
    exec(compiled.code, restricted_globals, local)
  File "meta_device_tracker.py", line 15, in <module>
TypeError: Can't convert 'NoneType' object to str implicitly

My automation looks like this:

automation:
  - alias: "Update Mattes Meta Tracker"
    trigger:
      platform: state
      entity_id: device_tracker.matte_api, device_tracker.matte_fing, device_tracker.matte_wifi, device_tracker.matte_bt
    action:
      service: python_script.meta_device_tracker
      data_template:
        entity_id: '{{trigger.entity_id}}'

Any idea what that could be?

When you start Hass up the device_tracker the script is looking for hasn’t been initialized yet.

I’m no longer using this script since bayesian binary essentially does the same thing combining device trackers. But it needs to check for null first if you want to get rid of the error