Combine "Automation" with "Scripts" (Object-oriented inhertiance)

Hello everyone,
I am pretty new to home assisstant but already love the possibilities it gives me with my smart home setup (Philips Hue, Sonos Speakers, Logitech Harmony, Flic Hub, Shelly Covers etc.).

I am currently trying to create an automation for my Hue lights. The plan is as follows:

  • I have ~50 lights in my appartment in differenz zones (rooms)
  • there are multiple motion sensors, each one in every room
  • now I would like to create an automation which gets feeback from the motion sensors, hands over the specific room to a script which will then be called in order to get the details about the light setup (brightness, color, duration etc.)
  • so I have to setup a automation for each motion sensor, but they all can call the scripts which I then define for each time framte of the day I want the lights to be

Can someone maybe help me to understand why I get an error message with the following setup:

Script: Light_Day:

sequence:
  - service: light.turn_on
    metadata: {}
    data:
      color_temp: 360
      brightness_pct: 100
    target:
      entity_id:
        - script-var-zone
mode: single
fields:
  script_var_zone:
    selector:
      entity:
        multiple: false
    name: script-var-zone
    description: Übergabe der anzusteuernden Zone aus der Automation an das Script
    required: true

Automation: Motion Sensor which shall call the script:

alias: "Auto: Light_Day - Motionsensor - Wohn- und Esszimmer"
description: >-
  Die die Bewegung des Motionsensors greift auf die Automation zurĂźck, welche
  das das Script "Day" fĂźr die Tageslichthelligkeit aufruft und damit die
  Zielentity ansteuert
trigger:
  - type: motion
    platform: device
    device_id: 87c73bfc24d541d9f934d3a103799992
    entity_id: 9a9cb0415dec0c274de38175d27c0ef1
    domain: binary_sensor
condition: []
action:
  - service: script.light_day
    data:
      script_var_zone: light.wohn_esszimmer_hue_zone
mode: single

When I run the script I get the following error message:

  • Title: Error running action
  • Message: Template rendered invalid entity IDs [‘script-var-zone’]

So I guess I did something wrong with the variable… but don’t know how to resolve it.

I run HASS 12.4 with the 2024.6.3 core. The hue lights are bined together through a Zigbee Group (Zone) within the Hue App itself, since I use the Hue Hub.

You are using a light turn on to call a script.
Light turn-on needs to turn on lights.
Scripts are a service calls and you can add the data to the end of them.

That isn’t even an entity_id

1 Like

when you try to use an inbound argument (field), first, you need to use the variable name, not hte firnedly name (script_var_zone) then also you need to evaluate it in a template so that the system knows you mean the variable script_var_zone and not the string “script_var_zone”

i presume {{script_var_zone}} will always evaluate to a light entity. otherwise you’ll hit the issue that isn’t not going to be goodenough, sir. (@Sir_Goodenough :wink: )

sequence:sequence:
  - service: light.turn_on
    metadata: {}
    data:
      color_temp: 360
      brightness_pct: 100
    target:
      entity_id: |
        {{ script_var_zone }}
mode: single
fields:
  script_var_zone:
    selector:
      entity:
        multiple: false
    name: script-var-zone
    description: Übergabe der anzusteuernden Zone aus der Automation an das Script
    required: true
1 Like

one other thing… on your automation trigger, you’re using the device_id.

since you’re just getting started, do yourself a favor and avoid them. here’s info on why and how. be kind to your future self…

4 Likes

@armedad thanks for your help so far (also @Sir_Goodenough thank you too).

What do you mean with: need to evaluate it in a template"? What template?

Yes, your assumption is correct: I want to always evaluate to a light entity.

Since I am a really new beginner, is it maybe advisable that I smart with less complex automations? E.g. automate each sensor with one automation which triggers all needed szenarios throught this one automation and will be configured via the GUI by me?

And thanks for the tip in regards of the entitiy_IDs :slight_smile:

The code I put above has the templating done for you. Take a look and try it.

For more general info about templates, here’s a page about templating

1 Like

each person learns their own way, but if you’re a total beginner, i do think there’s a lot you can learn by doing automations that are most or all done via the gui, then go look at the resulting yaml and learn from that.

the gui is actually quite powerful…

Hi @armedad,
I did take a step back and am pretty happy just with Automations.

Thanks again for your help and the tips :slight_smile: