CA Title 24 vacancy sensor blueprint? Need nested automation

I have never written blueprints before. I would like to implement a CA title 24 vacancy sensor. They normally work like this :

  1. initially turn on the load by manually pressing a physical switch, or using HA, or using a physical remote. The method of turning on the switch is not really relevant to this automation.
  2. after a (configurable) set amount of time elapses and there is no motion detected from the motion sensor, turn off the load/switch
  3. if there is motion shortly after step 2 (typically within 0-30s or 0-60s), turn the load/switch on. This step is only ever applied if step 2 has turned off the load, never in any other case.

I can easily implement step 2 with a basic automation if I have a smart switch and motion sensor device. However, I don’t see how to implement step 3. This requires a nested automation. I don’t think it’s possible with the GUI, but is it possible with a blueprint, and if so, how ?

That’s easy. (even in the UI) Just add another trigger based on motion and a condition that the automation only runs if the switch (load) was turned off recently (within your preset timeframe) You can exclude the manual turning on from the automation and do that outside of this.

1 Like

Dave, thanks for your reply.

Can you please clarify how to do this with the GUI ? I thought all the triggers in the GUI were logical OR . Is that not the case ?

Or did you mean that I should write a separate automation for step 3 ? But if so, how would I pass the time around between the two ? I guess I have only written some fairly basic automations despite having used HA for the last year.

Here is an example of automation I have for step 2 .

alias: >-
  Turn office light off when ZWave motion sensor detects no motion for 10
  minutes
description: ""
trigger:
  - type: no_motion
    platform: device
    device_id: 6b90264c3367435453d6c7deb88ab3ed
    entity_id: binary_sensor.office_motion_sensor_motion_detection
    domain: binary_sensor
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.office_ceiling_light
mode: single

This is correct.

If you use trigger_id’s then you can have a nice tidy automation with everything in the one place.

Have a trigger for step 2 as you have above (although I would steer away from using device, just use entity_id) and give this a trigger_id of ‘off’.

Create a second trigger with trigger_id ‘on’, being the motion sensor state to ‘on’, but with no for: section.

In your action, use the choose: function based on the particular trigger_id. If the trigger_id is ‘off’, turn off the load.
If the trigger_id is ‘on’ then have a subsequent condition that the load’s state change must be within the timeframe you wanted (within the last 30 seconds, or whatever you want)

1 Like

Thanks again ! The trigger id is very nifty.
I still have 2 questions :

  1. what type of trigger do I use for entity instead of device ?
    There is no “entity” drop-down in the GUI for the types of triggers. I have been using Device triggers for most of my automations. Is there something wrong with it ?
  2. unfortunately, I just couldn’t figure out the part about checking the time since step 2, even with the link you provided . Is this part doable in the GUI as well ?

1:
Use ‘state’

2:
I haven’t actually done this myself, only look at that example. I’ll have to have a play with the template dev tool to figure it out, but yes, it can be done in the UI as you can add YAML in there. See ‘edit in yaml’:

Try this as a template condition for your last part.

- condition: template
  value_template: "{{ as_timestamp(now()) - as_timestamp(states.switch.office_ceiling_light.last_changed)  < 30 }}"

It works for me.

If I get some time tonight I’ll post a full automation for you.

1 Like

Ok, so I knocked up this example completely in the UI but then selected ‘edit in YAML’ so I could post if here easily.

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.paradox_z14_main_shed_pir
    to: "off"
    id: "off"
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.paradox_z14_main_shed_pir
    to: "on"
    id: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kingsley_heated_bed_power
      - conditions:
          - condition: trigger
            id: "on"
          - condition: template
            value_template: >-
              {{ as_timestamp(now()) -
              as_timestamp(states.switch.office_ceiling_light.last_changed)  <
              30 }}
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kingsley_heated_bed_power




1 Like

Thanks once again.

I will give it a try !

Edit: this seems to work, though very short duration for the first trigger (seconds) don’t seem to work - or at least not quickly. That’s likely an issue with my motion sensor not reporting the state change fast enough. It’s a ZSE18.

Edit2: now I need to turn it into a blueprint, since I have 4 switches that need this automation and I don’t want to just duplicate the automation. That’ll be for another day, though.

This can be adjusted. See parameter 18:

1 Like

Thank you !
I think there is one remaining problem in the automation, though - if the switch is manually turned off, I don’t want it to turn back on, even if there is motion in the next 30 seconds. This is to mimic the behavior of a real California Title 24 vacancy switch.

How are you manually turning it on/off? Via a physical device button or the HA GUI? Depending on this we should be able to create an additional condition.

Sometimes I do it from the HA GUI, but rarely, maybe 5% of the time. Most frequently, I do it using an X10 remote, either RF remote + RF to PLC transceiver or IR remote + IR to PLC transceiver. Those remotes end up transmitting signals on the power line that HA captures.

The 4 switches in question are X10 XPS3 and all control recessed lighting . If I press the switches themselves (either to turn on or turn off) there is no feedback over the powerline unfortunately, so HA cannot know the real status of the switch in that case.
This is a deficiency in the X10 hardware unfortunately, nothing that can be solved in software. I have not found anything equivalent to the X10 IR543 (IR to PLC transceiver) in the Z-Wave world, or by any other vendor than X10, really.

I never operate the first XPS3 switch directly, as it’s located behind my office door. For the 3 other XPS3 switches in my home theater, I sometimes turn them on/off directly at the switch. It’s important for the lights not to turn themselves on accidentally in the home theater as it’s a windowless room with a projector and screen, and any amount of light significantly impacts the picture. Typically I turn off the lights with my ARRX18G learning IR remote from the couch, though, but again, not always :slight_smile:

I think the automation shouldn’t necessarily need to know the state of the switch to work (especially since the XPS3 switch state is partially unknowable). It just needs to know the last time the automation’s “off” condition was triggered, record it into some variable, and then, if the “on” condition is triggered, have a condition that checks the difference between current time and the last automation-off (and not any other OFF source), before turning the switch back on.

When editing in YAML, you can determine when a light is turned off, if it was done manually (physically on the switch itself) using this as a condition:

condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"

Then use this in a simple automation to populate an input_datetime named “last_manual_off” with the current exact date and time.

Then, in any automations where the light would go on (from say motion detected), add as a condition in each of those automations to continue that automation to turn the light on only if this is true:

current datettime > (last_manual_off + (5 minutes or whatever))

Works like a charm for me - I was running into a situation where someone would turn a light off to then leave the room, but as soon as they walk towards the door the light would go back on again! So in my case I am essentially ignoring any motion sensed to turn the light on for 5 minutes after it was manually turned off. FYI, for my calculations above, it would only work intermittently, but I discovered I had to convert the datetimes to longs to then add the 300 seconds (5 minutes) to then compare the two longs (seems to be the most reliable accurate way to do it - it always worked after I did that). FYI from the get go - the above piece of logic always worked perfectly in determining if it was manually done.

FYI, I have found no other way to historically determine when the last time a light was turned off, if it was turned off manually or not. I think those trigger context details are never stored anywhere in the historical DB (would be great if they were, then I wouldn’t have to jump through these hoops!).

My code may be whacky, but the above works an saves properly, but when you go to edit the automation, in the visual editor it is not shown, you switch to “edit in yaml” and they suddenly reappear (they are there but hidden - weird!)

Hope that helps!

1 Like

KruseLuds,
Thanks. I will try to use it when automating non-X10 switches. I don’t think it’s possible to determine the state switch for X10 switches. Reason being they are write-only devices. You can’t query their state programmatically. For example, if HA restarts, it cannot know their state. When you press these switches directly there is no signal of any kind being sent anywhere. And it’s not possible for to do a state query on the switch. You could call them “not-so-smart” switches. Yet they work with direct IR and indirect RF control, making them ideal for my home theater use, so I still have them. The motion sensor integration part remains to be fixed, though.

Sorry I misunderstood. Replace all the X10 stuff and control it all with your phone! :slight_smile:

I wouldn’t know what to replace X10 with. I like the X10 remotes very much. They can function without HA.

I especially like the IR543 which allows any IR remote to be used. No equivalent exists, AFAIK. I wish the switches could be queried, if course.

In the home theater which is a dark room, combined with my macular degeneration, I don’t find the phone to be a compelling UI. Certainly it’s much slower than tactile buttons on an IR remote, RF bu

Anyway, it should be possible to create an automation even for switches you can’t interrogate. The main relevant input are the motion sensor and time. The output is the switch state.

Let us know if you need more help implementing the condition to prevent the load being switched back on after being manually turned off. The idea from KruseLuds should work for you.

1 Like

Hey, if your load to turn on is a light, I would recommend this sensor light blueprint.

Very easy to use, with many configurable optional features, used by quite a lot of people (so pretty tested).

Not sure if you can turn on a switch with this, but maybe a scene or simply changing the switch type might work.

Dave,
Thanks for the offer. I do need more help. I don’t see how KruseLuds’ idea helps. Honestly, the automation never needs to determine the state of the switch. This should be obvious by reading my OP. I don’t need to know if the switch in the ON or OFF state for either step 2 or step 3.

For step 2, if there is no motion within the set time, the automation just needs to set the switch to OFF state. If it’s already ON (regardless of how it was turned on, physically, with a remote, or in HA), it turns it to OFF. If it’s already OFF (regardless of how it was turned off), it’s a no-op. Either way, it should behave as intended, without needing to know the switch state at any point.

For step 3, same thing. If there is motion between the time of step 2 was executed and, say, 30 seconds later, it sets the switch to ON. It has no need to know what the current switch state is.