How to access trigger to/from for condition under action?

I have the following trigger. I would like to filter further based on from:. How would I format the equivalent of from: true as a condition to be used under action?

trigger:
  - platform: device
    device_id: 88941889d6db7a90bc0c8c25b152f001
    domain: zwave_js
    type: zwave_js.value_updated.value
    command_class: 37
    property: currentValue
    endpoint: 1
    to: true

You should get in the habit of avoiding device triggers and device actions in automations. See further detail here.

A state trigger will give you trigger.to_state and trigger.from_state objects that you can access. If the device trigger is using a state as the trigger type you’ll also get those objects, but your device trigger isn’t even doing that.

I’m assuming you selected that trigger because you’re new to HA, but if you needed it for some specific reason I’d be interested to understand why.

The trigger variables that are available are documented here.

2 Likes

OK, you asked for it.

I have a Jasco 26931 / ZW4006 motion switch. I have it set to manual operation.

I want it to come on with motion and turn off after 2 minutes without motion. However, if I manually tap the switch on, then it should ignore motion and turn off after 1 hour. If I manually tap the switch off, then it should ignore motion for 10 seconds to allow time to exit without retriggering.

I have everything working, but I have to use 2 different triggers with hardcoded from: filter in order to accomplish the logic.

When the switch is activated via switch.turn_on, it actually generates 2 sequential command_class: 37 events. The first is off → on, The second is on → on. The second event does not occur when the switch is controlled with the physical paddle, but it could still show up if the switch was already on.

I set a flag when motion is detected so I can distinguish between motion/manual operation. Because of the 2 similar events, I can’t reset the motion flag until both events have been processed.

I don’t see a cleaner way to do this without writing a custom driver which incorporates the extra parameters and logic?

I also use these motion switches to trigger bathroom exhaust fans on a separate switch. The light comes on immediately, but the fan comes on 2 minutes later if motion is still detected. That’s my threshold between #1 and #2 or reaching into the shower to get a razor. No need to run the fan for quick in-and-out.

If it were me, I’d write the automation referencing only entity_id’s so that it would function with any hardware device(s), in case I replace the switch or motion sensor in the future. Or change zwave integrations, etc.

I would shy away from using a peculiar nuance of that device to determine physical interaction when you should be able to get that information instead from a native HA feature. See this post for how to access that info.

I’ll take a stab at an automation when I get some time in front of my computer.

Edit: if you want the light to stay on for an hour and it already turned on due to motion, do you want to turn it off then back on, or do you just want to turn it on again while it’s already on?

If you want to turn it on while it’s already on, you will have to use an event trigger.

The way it works now, tapping up when the switch is already on will always force/reset the manual stay-on-for-1-hour mode. Unless I were to sneak up on the switch, the motion is going to trigger first. It would be pretty lame to have to turn off and then back on again.

I saw the chart in your link. That’s a lot of work to hopefully hedge against obsolescence. It would be different if HA provided a guaranteed documented mechanism. Since I couldn’t find a way to check the from: in the action:, I created 2 separate stub triggers with different from: filters and call a common script. I don’t even need to pass the from: state because the only difference is that the from: true version clears a flag after calling the script. I can now use the same script to handle multiple switches. I’ve been experimenting with creating entity_id’s from a base name. I have helpers for all the parameters.

Here’s how to view exactly what variables are available within an automation:

  1. Make sure that your automation has been triggered at least once. You could start with a stub without any action.

  2. Select SettingsAutomations & scenes. You should be viewing the Automations tab.

  3. Click on your automation in the list.

  4. Click on TRACES at the upper right. You should be viewing the Step Details tab.

  5. Click on the Changed Variables sub-tab.

You’ll find all kinds of potentially useful information under this and trigger. I can now have a single automation that handles all cases with conditions under the action.