I have a piston that sends a GET request to an API and returns JSON which I then use to populate several global variables to be used in various pistons. One of these pistons sends Pushover notifications when music is being played from my Plex Media Server. I have everything working, but I am trying to figure out a way to limit the notifications to when the artist or album changes instead of having it fire for every individual track. I tried using the artist variable and setting a restriction that says the piston should only fire if that variable changes, but sthe variables update as each new track is played, and webCoRE sees this "update" as a change, even though the string itself is still identical.
Can anyone come up with a clever way of working around this?
This may not be the most elegant solution. When the music starts playing store the album name in a local variable (can be global but doesn't have to be) called something like currentAlbum. Then when you get an update check to see if the name sent equals the variable name. If they are different then send your notification. Something like this:
Trigger: On change
if thisAlbum != currentAlbum {
send notification
set currentAlbum = thisAlbum
}
So if the album name changes then send the notification and set a new name, otherwise ignore it.
The same thing happens. The current album gets updated with the same album name and webCoRE sees this as a change, so it fires the piston again.
The problem is that the piston is triggered to send the GET request each time a new track is played, so the artist and album variables get "updated" with the same information.
Can you show some code? I use the plex api hooks, which can then trigger a WC piston to run.
Unfortunately I haven't used webCore. This is what I might do in Rule Machine or Groovy.
When you say the piston fires - would that be like a trigger in RM? Then can you script actions in that piston event?
If so I don't see why this wouldn't work. Maybe I wasn't clear enough. You would have two variables. Let's call them currentAlbum and lastAlbum in this case. When the piston fires you put the album name in currentAlbum. Compare the two variables and if they are the same take no action. If they are different then send the notification and set lastAlbum = currentAlbum.
Since the notification is only sent when the two variables don't match it doesn't matter how many times it is called.
Can you show the code behind the piston you are setting up?
I was able to fix this by pausing the parsing for a few seconds after the JSON was retrieved. It looks like the values were actually changing depending on how fast webCoRE was setting the variables. With a slight delay, the values are consistent and my pistons aren't seeing changes.
Thanks for the help!
Ah, I hadn’t anticipated a race condition. Glad you got it sorted.
Are you polling PMSL from WC? Have a look at this example I wrote some time ago, which uses the plex webhook to call WC, to announce what is about to be played. Plex webhook & webcore - Solutions - Example Pistons - webCoRE Community Forum
I’ve since rewritten in full using nodered, which works more reliably.
I'm using Tautulli to trigger the webCoRE piston, which polls the Tautulli API for stream information then parses the JSON and sets global variables to use in different automations. I tried using PMS webhoooks, but the Tautulla API gave me access to more information than Plex did.
Tautulli can also send the JSON directly, but I found it easier to parse with webCoRE doing it through the API.