Cannot get PIR Automation working,

I have been playing with a PIR sensor and have found blueprints as a great way of connecting them.
However my efforts have failed to connect the PIR detected action to a remote connected relay…
My YAML,… as far as I can establish looks as follows:-

mode: restart
max_exceeded: silent
trigger:
  platform: state
  entity_id: binary_sensor.tas_ten_pir_switch2
  from: 'off'
  to: 'on'
action:
  - alias: Turn on the light
    service: light.turn_on
    target:
      entity_id: switch.tasseven_tasmota_seven
  - alias: Wait until there is no motion from device
    wait_for_trigger:
      platform: state
      entity_id: binary_sensor.tas_ten_pir_switch2
      from: 'on'
      to: 'off'
  - alias: Wait the number of seconds that has been set
    delay: 25
  - alias: Turn off the light
    service: light.turn_off
    target:
      entity_id: switch.tasseven_tasmota_seven
id: '1712777369343'
alias: Office PIR
description: Office Motion Detect

I can trigger the sensor,… and the movement is acknowledged,… and I can activated the relay, which changes state,…
However I cannot tie the trigger to the relay,…
What am I doing wrong? Also why did my binary sensor only show-up in HomeAssistant, until I added a relay,… and then detached it in Tasmota…? Can I just install,… and setup for autodiscovery a single binary switch… ie the PIR sensor.
Many Tx

Light.turn_on with a switch does not work, use switch.turn_on

1 Like

This is an automation, not a Blueprint.
But learning automations is the first step in learning to do blueprints, and automations are perfect for single tasks like this one.

1 Like

Have a look at the cookbook for motion:

1 Like

Just when you think your getting somewhere,… you realise how little you actually know…
Tx guys,… I have tried suggestions,… and played around with various combinations of settings, looked at more forum posts and wike than I can list here… but still to no avail…
My tasmota ‘D1’ sends the following MQTT message…

07:52:16.277 MQT: stat/Tas_Ten_PIR/MOTION = ON
07:52:16.282 MQT: stat/Tas_Ten_PIR/RESULT = {"Switch2":{"Action":"ON"}}
07:53:15.245 MQT: tele/Tas_Ten_PIR/STATE = {"Time":"2024-04-13T07:53:15","Uptime":"1T13:55:10","UptimeSec":136510,"Heap":25,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"ON","Wifi":{"AP":1,"SSId":"DiyNet0","BSSId":"9C:C9:EB:51:86:8F","Channel":2,"Mode":"11n","RSSI":44,"Signal":-78,"LinkCount":1,"Downtime":"0T00:00:04"}}
07:53:15.252 MQT: tele/Tas_Ten_PIR/SENSOR = {"Time":"2024-04-13T07:53:15","Switch2":"ON"}
07:53:38.217 RUL: SWITCH2#STATE=1 performs "publish stat/Tas_Ten_PIR/MOTION ON"
07:53:38.223 MQT: stat/Tas_Ten_PIR/MOTION = ON
07:53:38.228 MQT: stat/Tas_Ten_PIR/RESULT = {"Switch2":{"Action":"ON"}}

and I can see these messages appear on my MQTT Explorer window…
So I have come down to some basic YAML, as follows

automation:
  trigger:
    - platform: mqtt
      state_topic: "stat/Tas_Ten_PIR/MOTION"
      # Optional
      payload: "ON"
  action:
    - service: "switch.turn_{{ trigger.to_state.state }}"
      target:
      entity_id: switch.tasseven_tasmota_seven
mode: single

I have edited this code many times,… and ‘reloaded automations’,… but it still does not work,… I would like to add a basic timer to this… but I am clearly struggling with the basics at some level,… can anyone help further.
Also are there any debug tools,… to assist with code generation…?
Many tx

As I mentioned in my post here, not all sensors report ‘OFF’
Hence you cannot use trigger.to_state.state

I guess that would make it something like this:

alias: Light on Motion
description: Off when no motion for 3 mins
trigger:
  - alias: When motion detected  [Pir] was trigged by MQTT
    platform: mqtt
    topic: stat/Tas_Ten_PIR/MOTION
    payload: "ON"
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.tasseven_tasmota_seven
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasseven_tasmota_seven
mode: restart

Note that this automation is in ‘restart’ mode, as you want to reset the timer every time the automation is triggered

Many tx for your help/time,… have cut and paste your YAML,… into my automation.yaml file as shown above,…
And yes understand the ‘restart’ ,… have seen that in some of the posts I have read… reloaded automations,… even rebooted HA,.
However,… my relay/switch still does not change state when the PIR detects movement…
Yours,… Confused…

Does it trigger at all?
(check the trace)

You can also just paste it in the UI
(automation → three little dots → edit in yaml)

Hi,… cleaned my automations.yaml file out… so zero content.
Followed:- (automation → three little dots → edit in yaml)
and I copy->paste your YAML directly into the editor… deleting the preformed yaml text that appears as default,… ( so far so good as they say ).
However when I come to save my new YAML file I get message:-

Message malformed: extra keys not allowed @ data['action:']

I have done a quick search for what may be causing this,… but have found nothing concrete or definitive… Pls assume muppet level pre knowledge,… I need the idiots guide help pls
Yours,… ‘getting excited’, thankyou
diyhouse

sight…double semicolon…
my typo (yaml is very sensitive to these kind of typos, as well as wrong indentation)

I corrwcted my yaml now

PS: once you pasted in into the automation, you can use the same 3 dots to swicth back to gui mode :thinking:

Well I have it working… my final working code looks as follows

alias: Light on Motion
description: Off when no motion for 3 mins
trigger:
  - alias: When motion detected  [Pir] was trigged by MQTT
    platform: mqtt
    topic: stat/Tas_Ten_PIR/MOTION
    payload: "ON"
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.tasseven_tasmota_seven
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasseven_tasmota_seven
mode: restart

I did follow your instructions for a double colon,… this however failed, with formatting issues as before.
I tried some other variants of double colons to no avail…
Then based on some of your other edits I replaced the line

action: []

this somehow ‘satisfied’ the parser,… and like magic the relay changed state…
Finally a working Motion sensor time-out…
I subsequently looked in the automations.yaml file, to then find my

’ # single square braces

have been removed and I am now left with:-

action:

Most confusing… head scratch time… can anyone pls explain…
All said… I am very grateful for your help and patience,… I have been trying this and that for many weeks,… and not managed to find a solution until today…
Thankyou…

Very good,
Don’t foget to mark it solved by hitting the ‘solution’ button on the topic that solved it for you :wink:

is anyone able to explain why square brackets entered ‘fix’ the parser,… but then get removed??
What is the parser looking for and what is the correct format??

Many Tx

Sorry, not a clue…

Square brackets in YAML denote a list (among other ways to specify a list). Since the action: is expecting a list, adding in [] satisfies it by creating an empty list in the code. The reason the square brackets aren’t in @aceindy’s code is because he is giving an action: list by using - and then the calls to services. In YAML, - denotes a list item. Hopefully that made sense… It did in my own head at least :rofl:

Yes, it does get confusing very quickly, but, on the bright side, once you do learn YAML, you start to just get it and it makes sense eventually.

2 Likes

Yes,… I still know less than I thought,… but that does sort of make sense… and as you say I will try and get my head around it…
But many tx for the explanation…
rgds

1 Like

Just thinking a little more about my ‘YAML journey’, should I be creating my new automations from within the ‘create automations’ from settings → automations and scripts tabs,… or,. can/should I add via the ‘studio code server’, add-on…
which is the ‘recommended’ / best way to create and verify new scripts…
and which method is recommended to further edit scripts…?

You should use the GUI either way (using yaml or visual editor), don’t touch the file automation.yaml, as you might risk killing all your other automations when there is a mistake in it as there is no check on it when you save it (in fact, the studio code server add-on writes to the file after every change).

The automation editor will refuse to save when there are faults in the automation.

If you get more experienced and want to use templates in YAML, you can always switch from visual editor to yaml (and vise versa).

And if you use templates, you can still use the visual editor, but there will be a part that says it cannot use visual, and automatically fall back to yaml mode (for that section only).

The same applies to scripts :wink:

Thanks for the clarification,… I will bare this in mind in my future YAML automations…
Tx for your time and patience gain.
Rgds