0

I have this project: https://github.com/neuberfran/firebasefunction/blob/main/firebase/functions/smart-home/fulfillment.js It works well. But, for example, I want to implement a condition that if I have the garage closed and I said "close garage", the Home assistantt will alert me about it.

As shown in the photo below, I am using an rpi3/iot-device/back-end that controls the garagestate field. I need to know the best way to implement this condition, that is, read the value of the garagestate field and from that, know if I can open the garage or not:

enter image description here

neuberfran
  • 359
  • 3
  • 18

1 Answers1

1

You'd probably need to add an intermediary condition in your onExecute to return an error based on the Firestore state:

// ...
for (const target of command.devices) {
    const configRef = firestore.doc(`device-configs/${target.id}`)
    const targetDoc = await configRef.get()
    const {garagestate} = targetDoc.data()
    if (garagestate === false) {
       // garagestate exists and is false
       // return an error
       return {
         requestId,
         payload: {
           status: 'ERROR',
           errorCode: 'alreadyClosed'
         }
       }
    }
    // ...
}
// ...
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • 1
    Tks. But for now I had a regression.The function was deployed without errors. Issue is: "I couldn't find the garage. Please check in the app if this device is fully configured.". This is my new execute intent(I would be grateful if you can help more): https://gist.github.com/neuberfran/d35b02fafb6c6317051caeaaee49cc52 – neuberfran Mar 17 '21 at 01:47
  • 2
    My `// ...` meant to serve as placeholders. You should continue to use the `batch.update` command, just after the if-statement. This for-loop would replace the forEach you have in your code, but much of what you've done should remain unchanged. – Nick Felker Mar 17 '21 at 16:34
  • 1
    iwas successful in the first part of my issue. I get error when garagestate=false. But, I went to implement the option && (necessary condition to be added the main condition, because without it all the garage actions freeze when garagestate=false) in the if and I re-locked in the error of my previous answer. How to implement && curExec.command==='action.devices.commands.Close'? What is the best way to handle the errorCode: 'alreadyClosed' so that I stop getting the screen below and get a voice message on GHome?https://drive.google.com/file/d/1AuXEcTbDK_U6qVl6xq1U4NmC7Ww8vD-z/view – neuberfran Mar 22 '21 at 01:53
  • 1
    My last fulfillment execute intent Code: https://gist.github.com/neuberfran/6a1f21f28a155c5d122ee0e10b99116f – neuberfran Mar 22 '21 at 02:56
  • 2
    My code snippet shows that you should read the current fields from Firestore before going forward. You can add other checks to the same conditional. But ultimately you want to return the error the way shown above. – Nick Felker Mar 22 '21 at 16:30
  • 1
    My last question is: This following code is not working and is not calling the error screen: if (garagestate === false && command === 'action.devices.commands.Close'). If I put 'action.devices.commands.OpenClose', it calls the error screen both when I say open garage and close garage. This is not what I want. I just want the error screen to appear when I say close the garage. How would you help 1 more?https://gist.github.com/neuberfran/8314351f2a3467e7c19cf9b13d400048 – neuberfran Mar 23 '21 at 14:07
  • 2
    The command name you need to check for is [action.devices.commands.OpenClose](https://developers.google.com/assistant/smarthome/traits/openclose#device-commands). You can check the params (`reqCommand.execution[0].params.openPercent`) to see if it's a close or open request, where '0' is 'closed'. – Nick Felker Mar 24 '21 at 15:53
  • 1
    Tks. That's worked. Only one more think about screen error and not mensage by voice error: https://drive.google.com/file/d/1AuXEcTbDK_U6qVl6xq1U4NmC7Ww8vD-z/view – neuberfran Mar 25 '21 at 15:38
  • 2
    That might be the error you'll get. There's no way for you to customize it. – Nick Felker Mar 25 '21 at 17:04
  • 1
    I think I can create a error Class like: SmartHomeError: https://codelabs.developers.google.com/codelabs/smarthome-traits/#5 – neuberfran Mar 26 '21 at 00:59
  • 2
    You can create an error on your side, but it won't change the client-side error message. – Nick Felker Mar 27 '21 at 19:14