Can you have multiple actions in a single rule?

I’m having some issues with my rules. My understanding was I could have multiple actions at the top level. But it seems whenever I add a second rule it will cause the first rule to not work or be inconsistent. Here’s an example of what I am doing.

{
	"name": "Dining Room/Kitchen Rules",
	"actions": [
		{
			"if": {
				"equals": {
					"left": {
						"device": {
							"devices": [
								"Light Switch"
							],
							"component": "main",
							"capability": "switch",
							"attribute": "switch"
						}
				  },
				  "right": {
						"string": "on"
					}
				},
				"then": [
					{
						"command": {
							"devices": ["Cabinet Lights"],
							"commands": [
								{
									"component": "main",
									"capability": "switch",
									"command": "on"
								},
								{
									"component": "main",
									"capability": "switchLevel",
									"command": "setLevel",
									"arguments": [{"integer": 100}]
								},
								{
									"component": "main",
									"capability": "colorTemperature",
									"command": "setColorTemperature",
									"arguments": [{"integer": 4600}]
								}
								
							]
						}
					}
				],
				"else": [ 
					{
						"command": {
							"devices": [ "Cabinet Lights"], 
							"commands": [
							{
									"component": "main",
									"capability": "switch",
									"command": "off"
							}]
						}
					}
				],
				"sequence": {
					"then": "Parallel",
					"else": "Parallel"
				}
			}
		},
		
		{
			"if": {
				"remains":{
					"equals": {
						"left": {
							"device": {
								"devices": ["Dishwasher Sensor"],
								"component": "main",
								"capability": "accelerationSensor",
								"attribute": "acceleration",
								"trigger": "Always"
							}
						},
						"right": {
							"string": "active"
						}
					},
					"duration": {
						"value": {
							"integer": 3
						},
						"unit": "Minute"
					}
				},
				"then": [
					{
						"command": {
							"devices": ["Virtual Clean Status "],
							"commands": [
								{
									"component": "main",
									"capability": "switch",
									"command": "on"
								}
							]
						}
					}
				]
			}
		},
		
		{
			"if": {
				"remains":{
					"equals": {
						"left": {
							"device": {
								"devices": ["Dishwasher Sensor"],
								"component": "main",
								"capability": "contactSensor",
								"attribute": "contact",
								"trigger": "Always"
							}
						},
						"right": {
							"string": "open"
						}
					},
					"duration": {
						"value": {
							"integer": 2
						},
						"unit": "Minute"
					}
				},
				"then": [
					{
						"command": {
							"devices": ["Virtual Clean Status"],
							"commands": [
								{
									"component": "main",
									"capability": "switch",
									"command": "off"
								}
							]
						}
					}
				]
			}
		}
	]
}

So I’m checking

If Kitchen Lights are On, Then turn on Cabinet Lights, Else Turn off Cabinet Lights
If Dishwasher Motion detected for 3 minutes, Then Set Dishes Status to Clean
If Dishwasher is Open for 2 minutes, Then Set Dishes Status to Dirty

The first one works perfectly when I don’t have the other two in the rule.

Any idea what I may be doing wrong?

I notice that the first action is using a default trigger action on the device condition which means ‘auto’. The other actions have conditions set to ‘always’.

The docs are gloriously vague over exactly how `auto’ behaves when all the conditions are the same, but in this case they aren’t so I’d have thought that as the other two conditions explicitly trigger the first one probably won’t.

I could imagine the lights might go on and off, but not in immediate response to the switch, only when one of the other two actions triggeres. Is that the sort of problem you see?

I see. The first one does trigger when the others trigger. I didn’t even think of that. I accidentally left the triggers in but also just assumed since they were different actions on the same level they wouldn’t affect each other. Let me see if adding trigger to my first one helps.

Triggers are often misunderstood. A trigger is a prompt for an automation to execute - to work out what it needs to do and to do it.

In webCoRE the ‘triggers’ were conditions that were used to subscribe to device events of schedule timed events. The resulting events caused the pistons to execute from the top down rather like scripts, evaluating conditions as they went.

With Rules things are a bit different. They aren’t scripts. They subscribe to all the devices and timers they need to constantly keep themselves fully evaluated. The triggers are the events that are also followed by the Rules being executed. They are the events with the safety catch off.

It almost sounds like it would be better to separate my rules than. I’m getting weird behavior with multiple rules as if they’re conflicting because they’re going off. Your explanation of how json is different than a script kind of helped me realize where the issue could be.

1 Like

To me your example screamed ‘three Rules’, but it is a matter of taste. A lot does really come down to appreciating what the trigger events are and that they apply to the Rule as a whole.

1 Like

If I’m understanding right the way Rules work is if there are multiple actions and one gets triggered then all of them are evaluated? So if I had a rule saying when Switch A is On do A when Switch B is On do B there’s a chance for both to run, correct?

Yes that is my understanding, and as you get to specify the sequence in which actions are processed as Serial or Parallel they have to be executed together.