I have a rule which notifies me when a switch position changes. How can I write the message so it would tell me “The switch is (on or off)”?
{
"if": {
"equals": {
"right": {
"device": {
"devices": [
"-- ID switch --"
],
"component": "main",
"capability": "switch",
"attribute": "switch",
"trigger": "Always"
}
},
"left": {
"string": "on"
}
},
"then":
I think what OP wants is a string template to avoid having two automations, one for on and another for off.
I believe rules don’t support that so there’s no workaround but to have two.
It’s possible to make Rules API rules that have multiple actions.
Example:
{
"name": "Switch On/Off - Two Actions",
"actions": [
{
"if": {
"equals": {
"left": {
"device": {
"devices": [
"-- ID input switch --"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
},
"right": {
"string": "on"
}
},
"then": [
{
"command": {
"devices": [
"-- ID output switch --"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "on",
}
]
}
}
]
}
},
{
"if": {
"equals": {
"left": {
"device": {
"devices": [
"-- ID input switch --"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
},
"right": {
"string": "off"
}
},
"then": [
{
"command": {
"devices": [
"-- ID output switch --"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "off",
}
]
}
}
]
}
}
]
}
Yeah, that’s what I meant with one automation for on and another for off, even if it’s the same rule it’s two automations after all.
If Rules supported string templating like other platforms, OP could do something like “if the switch state changes, then send a notification with text 'the switch is {{switch.state}}”.
It is not possible to make ST App notifications using the Rules API.
Then I guess OP is not using the Rules API at all, first post says there is a notification and wants to change the message.
Anyway, @bloomer you’ll need two different messages, one when the switch is on and another when the switch is off.
In this instance, it isn’t necessary to have two actions, as any given rule is evaluated when the specified device:attribute changes. This means that turning the switch on or off will cause the whole rule to be evaluated. So for a single device boolean (on/off) such as this, an if:then:else will suffice, as follows:
(Note: I use the Pushover API (& webRequestor) for rule notifications, but have removed the full request strings for posting this example.)
{
"name": "Switch Example",
"actions": [
{
"if": {
"equals": {
"left": {
"device": {
"devices" : [
"{{SwitchA}}"
],
"component" : "main",
"capability": "switch",
"attribute" : "switch",
"trigger" : "always"
}
},
"right": {
"string": "on"
}
},
"then": [
{
"command": {
"devices": [
"{{webRequestor}}"
],
"commands": [
{
"component": "main",
"capability": "partyvoice23922.webrequest",
"command": "GET",
"arguments": [
{
"string": "{{pushoverOnMessage}}"
}
]
}
]
}
}
],
"else": [
{
"command": {
"devices": [
"{{webRequestor}}"
],
"commands": [
{
"component": "main",
"capability": "partyvoice23922.webrequest",
"command": "GET",
"arguments": [
{
"string": "{{pushoverOffMessage}}"
}
]
}
]
}
}
]
}
}
]
}