Trigger automation when sensor is read

Hi all, I’ve a command_line script (platform: command_line) that make a picture from a webcam and returns a number to HA.
This picture is taken 4 times a day, in the same moment I need to turn on the light, which is the best automation I can use?

thanks in advance

A simple state trigger on the entity will do it. Read the first paragraph there carefully.

With the limited details you’ve given, I’m assuming the light is being turned on to help expose the image… if that’s not the case ignore this.

Start with something like:

###automations.yaml

alias: timed lights camera action
description: ''
mode: single
trigger:
  - platform: time
      at:
        - "07:00:00"
        - "10:00:00"
        - "17:00:00"
        - "21:00:00"
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.camera_light
  - service: script.command_line_picture
  - service: light.turn_off
    target:
      entity_id: light.camera_light

Depending on the latency of you light source and how quickly your camera can adjust exposure etc, you may need to add a short delay between the light.turn_on and calling the script.

Hi all, I’d go with tinkerer suggestion. I tried something similar before posting without success. The automation I created is below. test_p is the command_line script, at the moment return always a fixed number (1).

- id: '1634155094656'
  alias: Test sensor read
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.test_p
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.photo_light
  mode: single

The automation will only be executed when the state changes, which obviously will never happen with your test.

thanks for the answer,
the important point is that the light must be turned on before the script returns. it must be trigger as soon as the script is executed (it takes 10seconds). You will know that the number is changed only after the photo has been taken.

And what triggers the script?

Hi Koying, it’s a Command line Sensor with scan_interval defined

thanks

Not sure I understand. You run a script every x minutes and you’d want the light to turn on before?
In the automation that runs every x, just turn on the light before the script, then.

1 Like

I’m with @koying… You’ve got your sequence reversed. You cannot use a state trigger to trigger the lights to come on before the state change event.

Please clarify whether you are using a script, a sensor, or both… post what you are using so we don’t have to guess.

1 Like

Hi Koying, the sensor has it’s own frequency and currently it isn’t controlled by an automation.
If I control it with an automation how I can disable the actual scan_interval?
In addition I don’t even know how to fire a Command line Sensor read from an automation as you suggest

thanks

There is command_line which is a sensor, so you’d need the value returned by the command. Apparently it’s not the case.

Then there is shell_command, which is a service that you can use in an automation, but for which you don’t need/care about the return value.
If you use that in an automation, you control the triggering with, e.g., a time pattern trigger, and you can turn on your light before, and even turn it off after, as the automation is blocked until the shell command has finished (iirc).

command_line sensors is the one I’m using but if I understood correctly I can’t sample it in an automation.
shell_command can be sample in an automation but then how I can pass the result to HA?

A possible alternative solution is to still use command_line sensors and in the external script running the following steps:

  • REST call to turn_on the light switch
  • Take the photo
  • run OCR
  • REST call to turn_off the light switch
    Unfortunately it doesn’t leverage to HA features
    What do you think?

Ok, if you really need the script to return a value to HA:

  • Set the scan_interval of the command_line to an absurd value (months, years…). It’s not currently possible to disable the scan interval altogether
  • In the automation, manually trigger the command_line refresh
  • Turn on the light before

Something like

  action:
  - service: light.turn_on
    target: 
      entity_id: light.my_light
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.my_cli_sensor

homeassistant.update_entity doesn’t to work.
The automation is triggered but the command_line sensor is not read
This is the configuration

sensor:
    - platform: command_line
        name: test_p
        command: "python3 /home/homeassistant/.homeassistant/test_p.py"        
        json_attributes:
           - temp
        value_template: '{{ value_json }}'
        scan_interval: 60000

automation:

 id: '1634155094656'
  alias: Test sensor read
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.rpiTemp
  condition: []
  action:
  - service: homeassistant.update_entity
    target:
      entity_id:
      - sensor.test_p
  mode: single

Your script returns a json? Didn’t you say before it returned a number?

Hi Koying, the external script is this

import json
import time
time.sleep(5)
out = {"temp": 1}
print(json.dumps(out))

Was the script working before?

Are you using a core install of HA (the venv one?)
If not, your script must reside under “/config”

Hi, I’ve installed HA with pip, the old installation guide for rpi