Python template

This may sound strange, but here it goes. Instead of using the cryptic template formats, I would like to be able to call a python module written using AppDaemon libraries from a template in HA. It’s so much easier to write things in python especially simple functions which is what we would be talking about. And there are times where it makes more sense to evaluate something in HA rather than wait for HA to update, then wait for AD to pick it up and process it to override what HA did.

You can already make calls directly into AppDamon from HA using custom events and the listen_event() cvall in AppDaemon:

listen_event()

Listen event sets up a callback for a specific event, or any event.

Synopsis

handle = listen_event(function, event = None, **kwargs):

Returns

A handle that can be used to cancel the callback.

Parameters

function

The function to be called when the event is fired.

event

Name of the event to subscribe to. Can be a standard Home Assistant event such as service_registered or an arbitrary custom event such as "MODE_CHANGE". If no event is specified, listen_event() will subscribe to all events.

**kwargs (optional)

One or more keyword value pairs representing App specific parameters to supply to the callback. If the keywords match values within the event data, they will act as filters, meaning that if they don’t match the values, the callback will not fire.

As an example of this, a Minimote controller when activated will generate an event called zwave.scene_activated, along with 2 pieces of data that are specific to the event - entity_id and scene. If you include keyword values for either of those, the values supplied to the `listen_event()1 call must match the values in the event or it will not fire. If the keywords do not match any of the data in the event they are simply ignored.

Filtering will work with any event type, but it will be necessary to figure out the data associated with the event to understand what values can be filtered on. This can be achieved by examining Home Assistant’s logfiles when the event fires.

Examples

self.listen_event(self.mode_event, "MODE_CHANGE")
# Listen for a minimote event activating scene 3:
self.listen_event(self.generic_event, "zwave.scene_activated", scene_id = 3)
# Listen for a minimote event activating scene 3 from a specific minimote:
self.listen_event(self.generic_event, "zwave.scene_activated", entity_id = "minimote_31", scene_id = 3)

You can create custom events in HA Automations as an action of an automation.

I’m talking about replacing the syntax of the templates in HA with a python function. So the template might be { my_convert_to_F(sensor.temp) }

where my_convert_to_F is a user written python function stored in some directory under HA.

1 Like

i think that part is there by the commandline sensor at the place from a template sensor.

unless you want to use templates at other places then in a sensor.
but actually all templates can be replaced by AD.

All sensors can be replaced by AD yes. But should they be. If all I want to do is convert a temperature or change a numeric value to a string, is it really something we want to burden AD with?

Well, if thats all you want to do then templates arent that bad.

like i said, if you dont like to do that with a template, you can do that also with a pythonscript and a commandline sensor.