Trigger if last trigger is more than....

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
Mikey
Posts: 9
Joined: 08 Feb 2013 21:25

Trigger if last trigger is more than....

Post by Mikey » 27 Dec 2017 20:08

I am trying to make a flow to make my phone disable and enable bluetooth, when it connect to my car.

My idea was to have the flow trigger when it connect to the car, then checks how many seconds has passed since last time it triggered, and if it more than 30 seconds, then it should disable bluetooth, wait 1 second, then enable bluetooth again.

I need it to check when it was the last time it did it, so it wont just disable and enable constantly.

Any suggestions on how to do it best? I haven't seen the light yet. :lol:

Mikey
Posts: 9
Joined: 08 Feb 2013 21:25

Re: Trigger if last trigger is more than....

Post by Mikey » 27 Dec 2017 21:34

I ended up with this, and it seems to be working, it just feels a bit clumsy.

Code: Select all

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<data version="1.34.0">
  <trigger type="bluetooth_device_connected">
    <useDefaultName>true</useDefaultName>
    <name>Bluetooth Device Connected: CAR</name>
    <enabled>true</enabled>
    <all>false</all>
    <deviceAddress>00:00:00:00:00:00</deviceAddress>
    <deviceName>CAR</deviceName>
  </trigger>
  <condition type="expression">
    <useDefaultName>true</useDefaultName>
    <name>Expression: global_carlastconnect == null OR triggertime-global_carlastconnect>60000</name>
    <expression>global_carlastconnect == null OR triggertime-global_carlastconnect>60000</expression>
  </condition>
  <action type="script">
    <useDefaultName>true</useDefaultName>
    <name>Script: global_carlastconnect = triggertime</name>
    <script>global_carlastconnect = triggertime</script>
  </action>
  <action type="set_bluetooth_state">
    <useDefaultName>true</useDefaultName>
    <name>Set Bluetooth State: Off</name>
    <enable>false</enable>
  </action>
  <action type="set_bluetooth_state">
    <useDefaultName>true</useDefaultName>
    <name>Set Bluetooth State: On</name>
    <enable>true</enable>
  </action>
  <action type="sleep">
    <useDefaultName>true</useDefaultName>
    <name>Sleep: 1s (keep device awake)</name>
    <duration>1s</duration>
    <keepDeviceAwake>true</keepDeviceAwake>
    <allowWakeupFromDeviceIdle>false</allowWakeupFromDeviceIdle>
  </action>
  <flow type="flow">
    <name>Bluetooth reconnect</name>
    <group>Car stuff</group>
    <enabled>true</enabled>
    <executionPolicy>PARALLEL</executionPolicy>
    <triggercontainer id="t1" x="70.0" y="52.5">
      <trigger>Bluetooth Device Connected: CAR</trigger>
    </triggercontainer>
    <actioncontainer id="t2" x="70.000015" y="472.5">Set Bluetooth State: Off</actioncontainer>
    <actioncontainer id="t3" x="70.000015" y="612.5">Sleep: 1s (keep device awake)</actioncontainer>
    <actioncontainer id="t4" x="70.00001" y="752.5">Set Bluetooth State: On</actioncontainer>
    <actioncontainer id="t5" x="70.00003" y="332.5">Script: global_carlastconnect = triggertime</actioncontainer>
    <conditioncontainer id="t6" x="70.00003" y="192.50002">Expression: global_carlastconnect == null OR triggertime-global_carlastconnect>60000</conditioncontainer>
    <connection from="t1" to="t6" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
    <connection from="t2" to="t3" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
    <connection from="t3" to="t4" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
    <connection from="t5" to="t2" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
    <connection from="t6" to="t5" type="TRUE" sourcePosition="SOUTH" targetPosition="NORTH" />
  </flow>
</data>

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Trigger if last trigger is more than....

Post by Desmanto » 28 Dec 2017 15:41

I don't understand why you must disconnect if within certain interval. But the flow is almost the best solution already.
You can remove the action script if you can move the script to the expression, but that is not necessary. As it doesn't speed up much.

The other is using sleep for the interval and use AEP wait. But that is not efficient, as the flow keep running for full interval (example 30 seconds).
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Mikey
Posts: 9
Joined: 08 Feb 2013 21:25

Re: Trigger if last trigger is more than....

Post by Mikey » 28 Dec 2017 15:53

Desmanto wrote:I don't understand why you must disconnect if within certain interval.
For some reason the radio in my car sometimes gets confused, and isn't receiving music information and such over bluetooth. Disconnecting and reconnecting makes something reset, and then it works. So every time I get into my car, automagic now detects it connecting to my car, then disables bluetooth, waits a bit, then enables it again. So far it works perfectly. The 60 second wait, is so it does not disable and enable over and over. Because after enabling bluetooth, it will again connect to the car, and the flow will be triggered again.
Desmanto wrote:The other is using sleep for the interval and use AEP wait. But that is not efficient, as the flow keep running for full interval (example 30 seconds).
I actually added more things to it already. So when it is triggered the second time, it starts up spotify on my phone, if it isn't already running. Might be a mess to have some kind of loop where it checks if it is connected again, to minimize waiting time until spotify is started.

Image

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Trigger if last trigger is more than....

Post by Desmanto » 29 Dec 2017 01:55

60 seconds should be enough for the reconnect. My bluetooth headphone also doing the same to my PC (phone is not affected). I have to reconnect once more to make sure the music streams thru.

If the flow can be simple and achieve what we need, then no changes needed, unless there is unexpected error somewhere. The flow is good already.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Mikey
Posts: 9
Joined: 08 Feb 2013 21:25

Re: Trigger if last trigger is more than....

Post by Mikey » 29 Dec 2017 08:35

Desmanto wrote:60 seconds should be enough for the reconnect.
Yes, but if I set it to wait a fixed amount of time, it would wait that long before continuing, and spotify would then first be started after 60 seconds, even if it reconnects in 2 seconds. And if I start spotify before disabling bluetooth, the spotify app gets confused instead. :lol:

I just like to continue tweaking things :P

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Trigger if last trigger is more than....

Post by Desmanto » 29 Dec 2017 17:44

Yep, that's why using glovar is the better solution.

For spotify hangs up, probably it is caused by the screen off? I see you want the flow to continue only when screen is off (screen on > false).
Try to change the condition to music active instead of spotify service.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply