# Data extraction

**URL:** <https://community.gladysassistant.com/t/extraction-de-donnees/8788>\
**Category:** Configuration\
**Created:** [March 22, 2024, 5:36pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788 "2024-03-22T17:36:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hizo](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Hizo](https://community.gladysassistant.com/u/Hizo)\
**Post date:** [March 22, 2024, 5:36pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788/1 "2024-03-22T17:36:48Z")

</div>

Hi,

I’m unsure about unexpected state changes of some Zigbee sensors.

Is it possible to extract their state changes and the times they occurred?

I send myself messages in the chat, but the history is too short to get a clear overview.

What would be the best solution?

Thanks

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [March 22, 2024, 6:05pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788/2 "2024-03-22T18:05:31Z")

</div>

Send yourself a Telegram message otherwise 🙂

---

<div class="post-metadata">

**Author:** ![Hizo](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Hizo](https://community.gladysassistant.com/u/Hizo)\
**Post date:** [April 19, 2024, 12:39pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788/3 "2024-04-19T12:39:56Z")

</div>

I’m following up on this topic.

It’s really not practical to send messages — isn’t there a better way to do this?  
It’s really tedious to check what’s starting and what’s not…

So, I figured I could maybe send an MQTT message that I’d log to a CSV, but oh well…

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [April 19, 2024, 12:42pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788/4 "2024-04-19T12:42:15Z")

</div>

> [@Hizo](#):
>
> I send myself messages in the thread but the history is too short to get a good overview.

Don’t you use Telegram? On Telegram, there’s no limit.

Otherwise make an HTTP call with the «&nbsp;HTTP Request&nbsp;» block, you can call a service like IFTTT that adds a row to a Google Sheet

There are plenty of possibilities 😛

---

<div class="post-metadata">

**Author:** ![Hizo](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Hizo](https://community.gladysassistant.com/u/Hizo)\
**Post date:** [April 19, 2024, 1:51pm UTC](https://community.gladysassistant.com/t/extraction-de-donnees/8788/5 "2024-04-19T13:51:24Z")

</div>

No, I don’t use Telegram, don’t want to add another piece of software…

Going through an external service doesn’t really appeal to me either…

So I made an MQTT message that sends me the name of the sensor or whatever that gets activated; I append a line to a CSV including the date and time.

For those who are interested, my bash code:

```auto
#/bin/bash

############################################
## Sending PC temperature to Gladys ##
############################################

### Variables to customize
# Connection to the MQTT broker
User="gladys"
Pass="MonMDP"
Host="Le_PC_avec_Gladys"

# Device topic address
Topic="gladys/master/device/mqtt:maison:Informations/feature/mqtt:maison:Informations:Texte/text"

CSVFile="/home/hizoka/SurveillanceActiviteGladys.csv"
### Variables to customize

function CommandCheck()
{
    # Function that checks for the presence of required commands
    # Parameter: Command name and package (optional) separated by :
        # E.g.: mosquitto_pub:mosquitto-clients

    # If no parameter is given
    [[-z ${1}]] && return 1

    # Limit variables to the function
    local Command Package Parameter ReturnValue
    ReturnValue=0

    # Process all parameters
    for Parameter in "${@}"
    do
        Command="${Parameter%%:*}"
        Package="${Parameter##*:}"

        # If the command does not exist
        if ! which ${Command} &>/dev/null
        then
            # Return value set to 1 to display all echos before stopping
            ReturnValue=1

            echo -e "[${ROUGE}Erreur${RAZ}] Arrêt du script car la commande ${BLEUFONCE}${Command}${RAZ} est introuvable." 1>2

            # If a package is given
            [[${Package}]] && echo -e "Pour ubuntu : ${FUCHSIA}sudo apt-get install ${Package}${RAZ}" 1>2
        fi
    done

    return ${ReturnValue}
}

# Colors for output display
FUCHSIA="\\e[1;35m"
RAZ="\\e[m"
BLEUFONCE="\\e[1;34m"
ROUGE="\\e[1;31m"
ORANGE="\\e[1m\\e[38;5;202m"

# Check for required commands which, if missing, cause the script to stop
# Requires the mosquitto_sub command from the mosquitto-clients package
! CommandCheck "mosquitto_sub:mosquitto-clients" && exit 1

# Infinite loop required if the program is launched with @reboot in cron
# Otherwise you should add a sleep before calling the script
while true
do
    # Loop continuously listening to the Gladys device topic
    # Retrieve responses into the variable Value
    while read Value
    do
        # To avoid logging duplicate consecutive lines
        if [[$(tail -n 1 "${CSVFile}") != "${Value};$(date +'%d/%m/%y');$(date +'%H:%M')"]]
        then
            echo "${Value};$(date +'%d/%m/%y');$(date +'%H:%M')" >> "${CSVFile}"
        fi
    done <<(mosquitto_sub -h "${Host}" -u "${User}" -P "${Pass}" -t "${Topic}")

    sleep 1
done

```
