Working with remote/mobile coordinates

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
janpep
Posts: 212
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Working with remote/mobile coordinates

Post by janpep »

In Domoticz you can make use of the location coordinates of your home as entered in your settings.
At the moment I use these coordinates to get and register e.g. local wheaterforcast, stookalert, earthquakes, airplanes.
It made me think that it could be nice to be able to use your remote/mobile coordinates under certain circumstances? You can use your imagination. So, I gave it a try.

Of course you are depending on some method to bring your remote coordinates over to Domoticz. But then you can work with it as you like.

In fact it is a two step process.
1. Bring your coordinates from your phone to domoticz and place it in a uservariable (only when you are not at home).
2. A script that is triggered by change of the uservariable, picks it up and continues to work with it. (You can think of restricitions to be build in.)

Needed:
  • For the example I have created a custom text device, that gets a link to google
  • I created a string uservariable to store the remote latitude and longitude in one string.
    RemoteCoordinates-1-JanPep.png
    RemoteCoordinates-1-JanPep.png (9.76 KiB) Viewed 602 times
  • The script, as it is, makes use of persistent data in a variable 'prevLatLong' in global_data. His holds the previous position to compare if the location has changed. This prevents unneeded updates.
Step 1:
I use the Macrodroid app on my smartphone. You may have or want other options for this, but this is what I use to explain and show it.
It's a bit of a puzzle, but I'm getting better at it. A lot is possible. See the explanation below the picture.
RemoteCoordinates-2-JanPep.png
RemoteCoordinates-2-JanPep.png (197.73 KiB) Viewed 602 times
Explanation of these steps in Macrodroid:
  • Macrodroid is triggered by a timer every 15 minutes only when its boolean varable that I am not at home is set (by geofence).
  • It cleans all the local variables used in the macro.
  • It forces to update the current location.
  • It stores the coordinates in a local string variable 'location'.
  • it waits until the variable is changed.
  • When location is retreived, it goes into a loop.
  • Update Domoticz uservariable with the location string.
  • Obtains the 'httpResonse', Parse the JSON to 'json' and gets the status in variable 'dzStatus'.
  • When dzStatus = OK, we get oout of the loop, If not we wait 5 seconds and try again.
The variable (type 2 = string) is updated with the following command:

Code: Select all

https://yourip:yourport/json.htm?type=command&param=updateuservariable&vname=<YOURVARIABLENAME>&vtype=2&vvalue=<YOURLATLONGSTRING>
Tested this afternoon and until now works very well and very fast.

Step 2:
In Domoticz now we see the uservariable that is set.

The scripts is triggered by the update of this variable and puts the coordinates in a hyperlink that is shown in the custom text device.
Prevents update when location is not changed.

v-myLocation

Code: Select all

-- 11-06-2024 Script by Jan Peppink; hppts://ict.peppink.nl
-- Result:  Work with your remote coordinates. (Set link to google maps that point to your current location.)
-- Makes use of variable prevLatLong in global_data.
-- Triggers on changed uservariable.

--- Your settings ----------------------------------------------------------------
-- First set the used device index numbers and variables you might want to change.
local locationVar = 'youruservariable'    --Name of your location variable, that holds "lat, long"
local loc_idx = 9999    --idx of text device
local testing = 1       --Use Fake coordinates for demo. 0 = for production mode :-)

--------------------------------------------------------------------------------
return {
	on = {
		variables = {
			locationVar,
		},
	},
	logging = {
	    -- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
        level = domoticz.LOG_INFO,
        --level = domoticz.LOG_DEBUG,
        marker = "MobileLocation--"
    },
	execute = function( dz, triggeredItem )
        -- Set Local environment=================
        --local _u = dz.utils       -- Holds subset of handy utilities.
        --local _h = dz.helpers     -- Holds the global functions.
        local _d = dz.globalData  -- Holds the global data.

        local latlong = ''
        if testing == 1 then
            latlong = '52.373224302364264,4.891381706567313' --get test location.
        else
            latlong = triggeredItem.value -- Get the LastLatLong from uservariable.
        end
        

        -- Use color variable for hyperlink font color
        htmlColor = 'Blue'

        -- Local Functions go here =============


        -- Now start to do something ============
        if _d.prevLatLong ~= latlong then
            --Location changed, so update
            local locationText = '<a href="https://maps.google.com/?q=' .. latlong .. '" target="_blank"><span style="color: ' ..  htmlColor .. '">Toon mijn locatie</span></a>'
            dz.log( 'Location is set to ' .. triggeredItem.value,  dz.LOG_DEBUG )
            dz.devices( loc_idx ).updateText( locationText )

            --Update and store this as the previous location
            _d.prevLatLong = latlong
        end
	end
}
-- That's All --------------------------------------------------
And the result:
RemoteCoordinates-3-JanPep.png
RemoteCoordinates-3-JanPep.png (7.45 KiB) Viewed 602 times
Just for fun: The build in testing mode points to the follwing location:
The Testlocation
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 212
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Working with remote/mobile coordinates

Post by janpep »

Updated the script v-myLocation.
  • Removed the prevLatLong from global_data (not longer used). This turned out not to be useful, because coordinates can easily vary slightly at the same location.
  • In stead now added 'loc_lasttimeAthome' as a boolean in data part of the script to keep track.
  • Hereby a distance < 50 meter is compared to home location coordinates is considered to be at home.
  • The Google map link will only be placed when not at home. Only update when changed.
  • No location updates when at still at home.

Code: Select all

-- 11-06-2024 Script by Jan Peppink; hppts://ict.peppink.nl
-- Result:  Work with your remote coordinates. (Set link to google maps that point to your current location.)
-- Triggers on changed uservariable.
-- 11-06-2024 Added distance from Domoticz location. Makes use of helper function 'calculateDistance' in global_data
-- 05-07-2024 Remove prevLatLong frp, global_data (not longer used)
-- In stead added loc_lasttimeAthome in datapart of the script to keep track.
-- Distance < 50 meter is considered to be at home.
-- Google map link will only be placed if not at home. Only update when changed.

--- Your settings ----------------------------------------------------------------
-- First set the used device index numbers and variables you might want to change.
local locationVar = 'youruservariable'	--Name of your location variable, that holds "lat, long"
local loc_idx = 99999	--idx of text device
local testing = 0	   --Use Fake coordinates for test/demo.

--------------------------------------------------------------------------------
return {
	on = {
		variables = {
			locationVar,
		},
	},
	data = {
		-- Persistant data to use in the next run of this script.
		loc_lasttimeAthome = { initial = true },
	},
	logging = {
		-- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
		--level = domoticz.LOG_INFO,
		--level = domoticz.LOG_DEBUG,
		marker = "MobileLocation--"
	},
	execute = function( dz, triggeredItem )
		-- Set Local environment=================
		local _u = dz.utils	   -- Holds subset of handy utilities.
		local _h = dz.helpers	 -- Holds the global functions.
		--local _d = dz.globalData  -- Holds the global data.

		-- Get location coordinates
		local lat = dz.settings.location.latitude
		local long = dz.settings.location.longitude

		local latlong = ''
		if testing == 1 then
			latlong = '52.373224302364264,4.891381706567313' --get test location.
		else
			latlong = triggeredItem.value -- Get the LastLatLong from uservariable.
		end

		local loc_nowAtHome = false
		local locationText = ''

		-- Split the latlong string to the coordinates.
		local position_table = _u.stringSplit( latlong , ',')
		local mob_lat = position_table[1]
		local mob_long = position_table[2]
		dz.log( 'Mobile location is set to ' .. mob_lat .. ' and ' .. mob_long, dz.LOG_DEBUG )

		-- calculate the distance in KM and in M		
		local mob_distanceKM = _u.round( _h.calculateDistance( lat, long, mob_lat, mob_long ), 1 )
		local mob_distanceM = _u.round( _h.calculateDistance( lat, long, mob_lat, mob_long ) * 1000, 0 )
		dz.log( 'Mobile distance is set to ' .. mob_distanceKM .. ' km. and ' .. mob_distanceM .. ' m.', dz.LOG_DEBUG )

		--Coordinates may vary a litthe, so is this at home?
		if mob_distanceM < 50 then
			-- Distance from home coordinates is less then 50M. I call it home.
			loc_nowAtHome = true
		end
		dz.log( 'The lastAtHome = ' .. tostring( dz.data.loc_lasttimeAthome ) .. ' and nowAtHome is ' .. tostring( loc_nowAtHome ), dz.LOG_DEBUG )
		
		-- Use color variable for hyperlink font color
		htmlColor = 'Blue'

		-- Local Functions go here =============


		-- Now start to do something ============
		if dz.data.loc_lasttimeAthome ~= true or loc_nowAtHome ~= true then
			--When location changed or not at home: Update.
			if loc_nowAtHome == true then
				locationText = 'Thuis'
				dz.log( 'Location is set to: thuis.',  dz.LOG_DEBUG )
			else
				locationText = '<a href="https://maps.google.com/?q=' .. latlong .. '" target="_blank"><span style="color: ' ..  htmlColor .. '">Toon locatie</span></a><br>Afstand: ' .. mob_distanceKM .. ' km.'
				dz.log( 'Location is set to: ' .. latlong,  dz.LOG_DEBUG )
			end
			--Update and store this as the previous location
			dz.data.loc_lasttimeAthome = loc_nowAtHome

			-- Update device text only when changed.
			if dz.devices( loc_idx ).text ~= locationText then
				dz.devices( loc_idx ).updateText( locationText )
			end
		end
	end
}
-- That's All --------------------------------------------------
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest