Scenes: Search within scenes via API

[31/01/2025] This post was taken from a feature request discussion

Nowadays the easiest way for this kind of task is to write a small script with ChatGPT, even if it gives you messy code we don’t care for this kind of one-shot task :smiley:

The API is simple:

What you’re interested in:

Example of a scene (I took an example from my own setup :stuck_out_tongue: )

{
    "id": "e352584c-1aaf-4cc7-a9a2-5caa6293419c",
    "name": "Allumage automatique toilettes journée",
    "description": "Allumage via capteur entre 8h et 20h",
    "icon": "loader",
    "active": true,
    "selector": "allumage-automatique-toilettes",
    "actions": [
        [
            {
                "type": "condition.check-time",
                "after": "08:00",
                "before": "20:00"
            }
        ],
        [
            {
                "type": "device.set-value",
                "device_feature": "zigbee2mqtt-ampoule-toilettes-light-brightness-brightness",
                "value": "215"
            }
        ],
        []
    ],
    "triggers": [
        {
            "type": "device.new-state",
            "device_feature": "zigbee2mqtt-capteur-mouvement-toilettes-motion-sensor-binary-occupancy",
            "value": 1,
            "operator": "=",
            "threshold_only": true
        }
    ],
    "last_executed": null,
    "created_at": "2024-03-30T07:36:29.090Z",
    "updated_at": "2024-07-05T07:08:47.591Z",
    "tags": [
        {
            "name": "Toilettes"
        }
    ]
}

I’m sure if you copy-paste this message into ChatGPT, you’ll have a working script :joy:

1 Like

Example:

ChatGPT’s response (I skimmed it; authentication is missing but otherwise it’s good):

Here is a Node.js script that uses axios to interact with the API, retrieves the list of scenes, and looks for those that use a specific device_feature, in this case « capteur_humidite » (« humidity_sensor »). If this device_feature is found in the actions or the triggers of the scene, the script logs the selector of that scene.

To run this script, make sure you have axios installed by running npm install axios.

Here is the code :

const axios = require('axios');

// Base URL of the API
const API_BASE_URL = 'http://votre-api.com/api/v1/scene';

// Function to fetch all scenes
async function fetchScenes() {
    try {
        const response = await axios.get(`${API_BASE_URL}`);
        return response.data;
    } catch (error) {
        console.error('Erreur lors de la récupération des scènes:', error);
        return [];
    }
}

// Function to fetch a full scene by its selector
async function fetchSceneBySelector(selector) {
    try {
        const response = await axios.get(`${API_BASE_URL}/${selector}`);
        return response.data;
    } catch (error) {
        console.error(`Erreur lors de la récupération de la scène ${selector}:`, error);
        return null;
    }
}

// Main function to find scenes that use "capteur_humidite"
async function findScenesWithHumiditySensor() {
    const scenes = await fetchScenes();

    for (const scene of scenes) {
        const sceneData = await fetchSceneBySelector(scene.selector);

        if (sceneData) {
            const hasHumiditySensor = sceneData.actions.some(actionGroup =\u003e
                actionGroup.some(action =\u003e action.device_feature === 'capteur_humidite')
            ) || sceneData.triggers.some(trigger =\u003e
                trigger.device_feature === 'capteur_humidite'
            );

            if (hasHumiditySensor) {
                console.log(`Scène avec \"capteur_humidite\" trouvée: ${scene.selector}`);
            }
        }
    }
}

// Script execution
findScenesWithHumiditySensor();

Instructions to run the script:

Make sure you have Node.js and axios installed in your project. If necessary, install axios with:

npm install axios

Replace ‹ http://votre-api.com/api/v1/scene › with your API’s URL.

Run the script by executing the following command in the terminal:

node votre-script.js

The script will print the selector of each scene that contains a device_feature with the value « capteur_humidite » (« humidity_sensor »).

1 Like