How to call service in python file when a sensor changes?

I’m missing the basic understanding on how HA runs a service when a sensor changes.
The code below runs when I manuially start

service: python_script.vvb_control

from SERVICES under Developer Tools.

vvb_control.py:

# VVB stikk control
service_data = {"entity_id": "switch.vvb_stikk"}
hass.services.call("switch","toggle", service_data, True)

What I miss is how the call be made automatic when (every time) i.e a temperature sensor changes.

Pseudo:
sensor.vvb_temp changes → call python_script.vvb_control

That’s not how python scripts work. Python scripts are a substitution for the yaml scripts. They run via service calls. If you want to call that script when something changes, you need to make a yaml automation that calls that python script.

There is no built in python engine for automation. You have to use PyScript or Appdaemon for that.

1 Like

Thanks a lot!!
Then I finally have the key to enter the house of fun :smile: